I am setting up a struts project and have been following an online tutorial at:
http://javaboutique.internet.com/tutorials/Struts/to get started. But my action forwarding is not working. When the submit button is pressed on the submit.jsp page (all code is below) submit.do appears in the browser but a blank page comes up - it should be reloading the same page with the updated javabean info. Any ideas anyone?
__________________________
__________
__________
__
submit.jsp
__________
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.
tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.
tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic
.tld" prefix="logic" %>
<html>
<head><title>Submit example</title></head>
<body>
<h3>Example Submit Page</h3>
<html:errors/>
<html:form action="submit.do">
Last Name: <html:text property="lastName"/><br>
Address: <html:textarea property="address"/><br>
Sex: <html:radio property="sex" value="M"/>Male
<html:radio property="sex" value="F"/>Female<br>
Married: <html:checkbox property="married"/><br>
Age: <html:select property="age">
<html:option value="a">0-19</html:optio
n>
<html:option value="b">20-49</html:opti
on>
<html:option value="c">50-</html:option
>
</html:select><br>
<html:submit/>
</html:form>
</body>
</html>
__________________________
__________
__________
struts-config.xml
_________________
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"
http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- ==========================
==========
======== Data Source Configuration -->
<!-- ==========================
==========
==========
== Form Bean Definitions -->
<form-beans>
<form-bean name="submitForm"
type="showcase.SubmitForm"
/>
</form-beans>
<!-- ==========================
==========
===== Global Exception Definitions -->
<global-exceptions>
</global-exceptions>
<!-- ==========================
==========
======= Global Forward Definitions -->
<global-forwards>
<!-- Default forward to "Welcome" action -->
<!-- Demonstrates using index.jsp to forward -->
<forward
name="welcome"
path="/Welcome.do"/>
</global-forwards>
<!-- ==========================
==========
======= Action Mapping Definitions -->
<action-mappings>
<!-- Default "Welcome" action -->
<!-- Forwards to Welcome.jsp -->
<action
path="/Welcome"
forward="/Welcome.jsp"/>
<action path="/submit"
type="showcase.SubmitActio
n"
name="submitForm"
input="/submit.jsp"
scope="request">
<forward name="success" path="/submit.jsp"/>
<forward name="failure" path="/submit.jsp"/>
</action>
</action-mappings>
<!-- ==========================
==========
========= Controller Configuration -->
<controller
processorClass="org.apache
.struts.ti
les.TilesR
equestProc
essor"/>
<!-- ==========================
==========
==== Message Resources Definitions -->
<message-resources parameter="MessageResource
s" />
<!-- ==========================
==========
==========
= Plug Ins Configuration -->
<!-- ==========================
==========
==========
========= Tiles plugin -->
<plug-in className="org.apache.stru
ts.tiles.T
ilesPlugin
" >
<!-- Path to XML definition file -->
<set-property property="definitions-conf
ig"
value="/WEB-INF/tiles-defs
.xml" />
<!-- Set Module-awareness to true -->
<set-property property="moduleAware" value="true" />
</plug-in>
<!-- ==========================
==========
==========
===== Validator plugin -->
<plug-in className="org.apache.stru
ts.validat
or.Validat
orPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-
rules.xml,
/WEB-INF/v
alidation.
xml"/>
</plug-in>
</struts-config>
__________________________
__________
__________
SubmitAction.java
_________________
package showcase;
import javax.servlet.http.*;
import org.apache.struts.action.*
;
public final class SubmitAction extends Action {
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
SubmitForm f = (SubmitForm) form; // get the form bean
// and take the last name value
String lastName = f.getLastName();
// Translate the name to upper case
//and save it in the request object
request.setAttribute("last
Name", lastName.toUpperCase());
// Forward control to the specified success target
return (mapping.findForward("succ
ess"));
}
}
__________________________
__________
__________
__
SubmitAction.java
_________________
package showcase;
import javax.servlet.http.HttpSer
vletReques
t;
import org.apache.struts.action.*
;
public final class SubmitForm extends ActionForm {
/* Last Name */
private String lastName = "Alfresco"; // default value
public String getLastName() {
return (this.lastName);
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
/* Address */
private String address = null;
public String getAddress() {
return (this.address);
}
public void setAddress(String address) {
this.address = address;
}
/* Sex */
private String sex = null;
public String getSex() {
return (this.sex);
}
public void setSex(String sex) {
this.sex = sex;
}
/* Married status */
private String married = null;
public String getMarried() {
return (this.married);
}
public void setMarried(String married) {
this.married = married;
}
/* Age */
private String age = null;
public String getAge() {
return (this.age);
}
public void setAge(String age) {
this.age = age;
}
}
Start Free Trial