First, we update the web.xml to direct all *.do URLs to the Struts controller.
Note that, by convention, Struts URLs have the suffix .do
Your web.xml will look like this:
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
We are directing the Struts ActionServlet to initialize itself from the WEB-INF/struts-config.xml file.
So let us now write this XML file.
Create a file struts-config.xml in your /WEB-INF folder, like this:
<struts-config>
<form-beans>
<form-bean name="helloWorldForm"
type="org.confucius.HelloWorldForm"/>
</form-beans>
<action-mappings>
<action path="/register"
type="org.confucius.HelloWorldAction"
name="helloWorldForm">
<forward name="success" path="/jsp/Confirm.jsp"/>
<forward name="error" path="/jsp/Error.jsp"/>
</action>
</action-mappings>
</struts-config>
This is a complicated looking configuration!
What are we doing here?
First we are declaring a Struts "Form" called HelloWorldForm and mapping it to a Struts "Bean" called "helloWorldForm"
Next we are mapping the URL /register to the action "HelloWorldAction" and to the form "helloWorldForm". Note that Struts will automatically add a ".do" suffix. So it expects the URL /register.do
An "Action" class is what the Struts Controller will direct the request to.
A "Form" class is what the Struts Controller will populate with the form parameters.
Finally, we have mapped the values returned by the HelloWorldAction, "success" and "error", to the relevant jsp Views.
In the /src/org/confucius folder, create the HelloWorldForm.java, like this:
package org.confucius;
import org.apache.struts.action.ActionForm;
public class HelloWorldForm extends ActionForm {
String firstName;
String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
This is the class where Struts will save the firstName and lastName from the form in HelloWorld.jsp
Next, create a new class HelloWorldAction.java in /src/org/confucius which looks like this:
package org.confucius;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class HelloWorldAction extends Action {
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws Exception {
HelloWorldForm helloWorldForm = (HelloWorldForm) form;
User user = new User(helloWorldForm.getFirstName(), helloWorldForm.getLastName());
if (user.validate())
return mapping.findForward("success");
else
return mapping.findForward("error");
}
}
This class is call by the Struts Action Servlet. It is passed the HelloWorldForm which is all setup with the request parameters from the form.
Instead of returning the actual View JSP, the Action class returns "success" or "error" - and Struts will select the appropriate JSP based on the configuration in struts-config.xml
Update your HelloWorld.jsp to point the form to register.do, like this:
<html>
<head>
</head>
<body>
<form method=post action="/HelloWorld/register.do">
First Name: <input type=text name=firstName>
<br/>
Last Name: <input type=text name=lastName>
<br/>
<input type=submit value="Register">
</form>
</body>
</html>
Finally, update your Confirm.jsp to pick the firstName and lastName from the Struts bean:
<html>
<head>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
</head>
<body>
Confirmation: Registration accepted for <bean:write name="helloWorldForm" property="firstName"/> <bean:write name="helloWorldForm" property="lastName"/>
</body>
</html>
Note that the HelloWorldForm bean is always available to the View. That is how Struts can make data available to the View. For example, you could have had a field called 'User' in the HelloWorldForm.java and HelloWorldAction could have populated it. It would have then been available in Confirm.jsp
As you can see, we have managed to do all the MVC wiring in struts-config.xml
We were able to connect the URL, the Form, the Action and the View declaratively in the XML.
We were able to pass data from the Form to the Action and to the View.
If you now build and deploy HelloWorld.war, the point your browser to:
http://localhost:8080/HelloWorld/jsp/HelloWorld.jsp
you will be able to play with the application and see the correct view show up, based on your inputs.
No comments:
Post a Comment