Struts 1.2
It
is an open source framework
It
is implementation of MVC design pattern.
Providing
a centralized file based configuration - struts-config.xml
Controllers in struts
It provides two components that make
up the controllers.
1.
Action
Servlet
§
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.
Simple Program Steps in Struts:
1.
Configure
ActionServlet in web.xml.
2.
Create
an Action class.
3.
Configure
the Action class in struts-config.xml
4.
Create
the View i.e. JSP page.
Step 1: Configure Front Controller (ActionServlet)
in web.xml
Standard Action
Servlet Configuration:
<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>
<load-on-startup>2</load-on-startup>
</servlet>
Standard Action Servlet Mapping:
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
DispatcherServlet will handle all the request coming with .do
pattern.
Step 2: create an Action class :
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;
public class RegisterAction1 extends Action{
@Override
public ActionForward execute(ActionMapping mapping,
ActionForm form,HttpServletRequest request,
HttpServletResponse response)throws Exception {
return
mapping.findForward("success");
}
}
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.
Step 3 :
configure the Action class in 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>
<action-mappings>
<action
path="/register1" type="mvc.RegisterAction1">
<forward
name="success"
path="/success.jsp"/>
</action>
</action-mappings>
</struts-config>
Step 4: create view success.jsp page
<html>
<head>
<title>Struts Demo</title>
</head>
<body>
<h1>Congratulations!!
You have registered
successfully
</h1>
</body>
</html>
How does struts work?
1.
It is the front
controller that works on behalf of application.
2.
DispatcherServlet
Handles all client requests and on
the basis of name determines which Action
will process the request.
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.
Using
Form Beans:
1. This bean is a class the
extends "ActionForm".ActionForms are java beans.
2. It will represent the data submitted by the user.
3. It is automatically populated when the input form is
submitted.
4. It can also be used
to validate the request data submitted by an HTTP request.
Defining a FormBean
package mvc;
import org.apache.struts.action.ActionForm;
public class
UserBean extends ActionForm
{
private String
email="";
private String
password="";
public String
getEmail() {
return
email;
}
public void
setEmail(String email) {
this.email
= email;
}
public String
getPassword() {
return
password;
}
public void
setPassword(String password) {
this.password
= password;
}
}
Design an Input form:
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<H1>New
Account Registration</H1>
<FORM
ACTION="registerBean.do" METHOD="post">
Email
address: <INPUT
TYPE="text" NAME="email"><BR>
Password:
<INPUT TYPE="password" NAME="password"><BR>
<INPUT
TYPE="submit" VALUE="Sign In">
</FORM>
</body>
</html>
Create an Action class:
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;
public class UserBeanAction extends Action {
@Override
public
ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest
request, HttpServletResponse response)
throws
Exception
{
String
res="invalid";
UserBean
userBean = (UserBean)form;
String
email=userBean.getEmail();
String
pass=userBean.getPassword();
if(email.equals("sonia@gmail.com")
&& pass.equals("sonia"))
res="success";
return
mapping.findForward(res);
}
}
Creating a success View : success.jsp
<html>
<head>
<title>Struts
Demo -- Success</title>
</head>
<body>
<h1>Congratulations!!
You have
registered successfully </h1><br>
Hello
${userBean.email }
</body>
</html>
Creating an error view: invalid.jsp
<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>
<a
href="registration.jsp">Please try again</a>
</h1>
</body>
</html>
Configuration in struts-config.xml
1. Configure form beans
<form-beans>
<form-bean
name="userBean" type="mvc.UserBean"/>
</form-beans>
2. Configure Action class
<action
path="/registerBean" type="mvc.UserBeanAction" name="userBean"
scope="request">
<forward name="invalid"
path="/invalid.jsp"/>
<forward name="success"
path="/success.jsp"/>
</action>
Using struts-tag library
1. Copy struts-xxx.tld files in
WEB-INF folder
2. Configure tag libraries in
web.xml file using following tags:
<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>
Using tag libraries
1. Redesign success and invalidate
page using <bean> tag.
success.jsp
<html>
<head>
<title>Struts Demo -- Success</title>
</head>
<body>
<%@ taglib uri="/tags/struts-bean"
prefix="bean" %>
<h1>
<br> <bean:write
name="userBean" property="email"/>
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 <bean:write name="userBean"
property="email"/>
is
wrong or not in proper form.
<br> or password is wrong.<br>
<a
href="registration.jsp">Please try again</a>
</h1>
</body>
</html>
Using struts-html tag
library
1. The text field names and the bean
properties are guaranteed to stay in synch.
2. The text field values can be
prepopulated based on the values in a bean.
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.
Create an input
form using struts <html> tags
---------------------------------------------
<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"%>
<html:form
action="registerBean1" >
<br>
<html:text
property="email" /><br>
<html:password
property="password" /><br>
<html:submit
value="sign up"/>
</html:form>
</body>
</html>
Configuration
in struts-config.xml
-----------------------------------
<action
path="/registerBean1" type="mvc.UserBeanAction"
name="userBean" scope="request">
<forward name="success"
path="/success.jsp"/>
<forward name="invalid"
path="/htmlform.jsp"/>
</action>
No comments:
Post a Comment