Académique Documents
Professionnel Documents
Culture Documents
2
----------it is a open source framework - implementation of MVC design pattern.
providing a centeralized file based configuration - struts-config.xml
Controllers in struts
--------------------It provides two components that make up the controllers.
1.ActionServlet
it is front controller.
it is a struts in-build Servlet so should be configured in web.xml
Each and every request come to this.
On the basis of request, it decides which Controller(Action) will handle
this request.
2.Action
it is back Controller.
it contains the business logic.
it is configured in the servlet -config.xml file using an <action> tag.
<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>
<load-on-startup>2</load-on-startup>
</servlet>
here the url-pattern specifies that all request with .do extension will be handled by
DispatcherServlet.
package mvc;
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;
When a request come for this Action class, it just returns a view
-- a logical name "success" is returned -that string is mapped to a view (a jsp file)in struts-config.xml file using a
<forward> tag.
<struts-config>
<action-mappings>
<action path="/register1" type="mvc.RegisterAction1">
</struts-config>
<html>
<head>
<title>Struts Demo</title>
</head>
<body>
<h1>Congratulations!!
You have registered successfully
</h1>
</body>
</html>
3. After processing the request Action class will return a key(String ) to the
ActionServlet.
4.ActionServlet use this key to determine the view that will present the result
to the user.
---------------------------------------------------------------------------------------------
Defining a FormBean
-------------------
package mvc;
import org.apache.struts.action.ActionForm;
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<H1>New Account Registration</H1>
<FORM ACTION="registerBean.do" METHOD="post">
Email address:
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;
@Override
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
String res="invalid";
return mapping.findForward(res);
}
<html>
<head>
<title>Struts Demo -- Success</title>
</head>
<body>
<h1>Congratulations!!
You have registered successfully </h1><br>
Hello ${userBean.email }
</body>
</html>
<html>
<head>
<title>Struts Demo -- Error</title>
</head>
<body>
<h1>
Either Emailid ${userBean.email } is wrong or not in proper form.
<br> or password is
wrong.<br>
configuration in struts-config.xml
-----------------------------------
<form-beans>
<form-bean name="userBean" type="mvc.UserBean"/>
</form-beans>
-----------------------------------------------
------------------------
<jsp-config>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
</jsp-config>
<html>
<head>
<title>Struts Demo -- Success</title>
</head>
<body>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<h1>
Congratulations!!
You have registered successfully
</h1>
</body>
</html>
invalid.jsp
-------------
<html>
<head>
<title>Struts Demo -- Error</title>
</head>
<body>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<h1>
Either Emailid
wrong.<br>
1. The textfield names and the bean properties are guaranteed to stay in
synch.
3. That is, the initial values of the form elements can be taken from a Java
object.
4. The forms can be redisplayed when they are submitted with incomplete or
incorrect values.
<html>
<head>
<title>Html Form Demo</title>
</head>
<body>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
</body>
</html>
configuration in struts-config.xml
-----------------------------------