The action class is a class
that is a subtype of org.apache.struts.action.Action. The action class that is
a part of controller provdes an entry point where we can start with our
application code to process the request. As shown in the architecture RequestProcessor
invokes the action by invoking the execute() method of the action , where the
action takes the responsibility to communicate with the model components and
prepare the data for the view that is required to prepare presentation. We need
to the view that is required to prepare presentation. We need to configure the
action I the struts configuration file using <action> tag configure the
action in the struts configuration file using <action> tag and map it to
a unique path so that the RequestProcessor can dispatch the request to our
action when a client makes a request with the specified path the following
snippet shows the mapping configuration in struts configuration file.
Code Snippet
<action path=”/login”
Type=”com.chandra.struts.LoginAction”/>
Now as we know the basic
elements of the struts architecture we will now start with a first simple
struts application that can explain about the basic configurations and as we
move on we will learn the other elements of the struts architecture like form
beans , exception handlers, plugins etc., The following post explains a basic
struts example.
Writing
your first Struts Application
Here to make the first example
simple we are implementing a login process which is a well known process, this
example consists of a Login.html which provides an entry point for this
application by providing login view allowing the user to make a login action,
the Login.html is shown in the following listing.
Login.html
<html>
<body>
<form action=”login.do”><pre>
User Name :
<input type=”text” name=”uname”/>
Password : <Input type=”password” name =”pass”/>
</pre></form>
</body>
</html>
The request make using the
Login.html is recived by the ActionServlet which then dispatches the request to
the RequestProcessor. The RequestProcessor after some request processing tasks
it invokes the execute method on the action class object, the LoaginAction
class to process the login request is shown in the following
LoginAction.java
Package com.chandra.struts;
Import org.apache.struts.action.*;
Import javax.servlet.http.*;
/**
·
@author Chandra
*/
Public class LoginAction extends Action{
Public ActionFrward execute(ActionMapping
am,ActionForm af,HttpServletRequest req,
HttpServletResponse
res)throws Exception{
String
uname= req.getParameter(“uname”);
String
pass=req.getParameter(“pass”);
LoginModel
lm = new LoginModel();
String
type=lm.validate(uname,pass);
If(type==null)return
am.findForward(“notValid”);
Return am.findForward(type);
}//execute
}//class
As show in the above
LoginAction execute() method is invoking the validate() method on the
LogingModel object where LoginModel is a plain java classs designed to perform
business logic operation in this case validate the login details, the listing shows
the LoginModel.java
LoginModel.java
Package com.chandra.struts;
Import java.sql.*;
/**
·
@ author Chandra
*/
Public class LoginModel{
Public String
Validate(String uname,String pass){
Connection
con=null;
Try{
Class.forName(“oracle.jdbc.driver.OracleDriver”);
Con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,”scott”,”tiger”);
Statement
st = con.createStatement();
ResultSet
rs = st.executeQuery(
“select type from userdetails where username=\”+uname+”\’and
userpass=\’”+pass+”\’”);
If(rs.next())
Return rs.getString(1);
}//try
Catch(Exception
e){}
Finally{try{con.close();}catch(Exception
e){}}
Return null;
}// }//class
As we have discussed in the
above sections we need to configure the action in to the struts configuration
file, here in this example we are configuring only one module i.e default
module and default module and default configuration file name of default module
is struts-config.xml file, for this example.
Struts-config.xml
<struts-config>
<action-mappings>
<action type=”com.chandra.struts.LoginAction” path=”/login”>
<forward
name = “notValid” path = “/Login.html”/>
<forward
name=”admin” path=”/AdminHome.jsp”/>
<forward
name=”user path=”/UserHome.jsp”/>
<action>
</action-mappings>
</struts-config>
The AdminHome.jsp which is presented in case if the login
details are validated as an admin type user
AdminHome.jsp
<html><body>
Welcome to the Admin Home page<br/>
User Name :<%=request.getParameter(“uname”)%>
</body></html>
The UserHome.jsp which is presented in case if the login
details are validated as user type
UserHome.jsp
<html>
<body>
Welcome to the Non-Admin User Home Page<br/>
User Name: <%=request.getParameter(“uname”)%>
</body></html>
Finally, as discussed we need to configure ActionServlet in
web.xml since it is also a normal servlet as any other servlet, the web.xml as follows.
<web-app>
<servlet>
<servlet-name>as</servlet-name>
<serlvet-class>org.apache.struts.action.ActionServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>as</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
Post a Comment
Post a Comment