Struts Learn the Fool-type introductory article

Source: Internet
Author: User
Tags config final locale version

Perhaps some people feel that struts is not easy to learn, it seems that some of the concepts in the MVC1, MVC2, mode ... I write this article is to let the person who has never contacted struts, can have a simple introductory guide, of course, the systematic learning struts is necessary, there are a lot of people enchanted Dongdong, that is something.

The case includes homepage, user login, website wizard page. As simple as this, there is no esoteric concept of struts, mainly by hands-on, and then by heart.

WEB Server uses TOMCAT4. Download struts1.1 to http://jakarta.apache.org , release zip file to c:\struts, copy C:\struts\webapps\struts-example.war to C:\ In Tomcat4\webapps, the boot Tomcat,war package is released as the Struts-example folder, the war package is deleted, and the Struts-example folder is renamed to test.
First, change the web-inf\web.xml into:

<?xml version= "1.0" encoding= "Iso-8859-1"?>
<! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.2//en" " http://java.sun.com/j2ee/dtds/ Web-app_2_2.dtd ">

<web-app>
<!-This is the controller (Controller) in struts, the system's instruction relays by it, both the Actionservlet class is responsible for, it reads configuration information from Struts-config.xml, and starts a thread automatically in the server background. If there are no special requirements (such as adding a language to turn the function), programmers can ignore this part, the replicable can be. -->
<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>1</load-on-startup>
</servlet>
<!--the servlet of the system can be mapped to a file with cool suffix, rather than a common. Jspdo etc, suffix name can be changed to any name, of course the name to health # ¥%! -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.cool</url-pattern>
</servlet-mapping>
<!--The default home page of the system is index.jsp, you can have multiple, system in order, similar iis-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Second, the test\web-inf\ struts-config.xml into:

<?xml version= "1.0" encoding= "Iso-8859-1"?>
<! DOCTYPE struts-config Public "-//apache Software foundation//dtd struts Configuration 1.1//en"
" http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd " >

<struts-config>
<!--Formbean is a concept of struts, essentially javabean, used to automatically store the values of each field in the page form and return to the form field at the appropriate time, without the need for request.getparameter as traditional ("FieldName" ), and is often used by the action in Action-mappings-->
<form-beans>
<!-later we'll add a UserForm class to store user information. -->
<form-bean name= "UserForm" type= "Test." UserForm "/>
</form-beans>
<!--here is a global switch (Forward) address that can be used by the entire system, similar to the window.location (' index.jsp ') in JavaScript, and similar to the various buttons on a TV controller, which can be transferred to a channel, Color palette and so on is based on the Struts Web application's control flow circulation. Typically, when an action is processed, it is forwarded to a JSP page for display. This is also the main point of the implementation of MVC in JSP. -->
<global-forwards>
<!--Failed.cool will be used as a servlet request to find the corresponding action in the action-mappings. -->
<forward name= "Failed" path= "/failed.cool"/>
<forward name= "regist" path= "/regist.jsp"/>
</global-forwards>
<!--remember the request for cool in web.xml? They are transferred here for processing. This is equivalent to the model section of struts, the model part is a more flexible place in struts. -->
<action-mappings>
<!--processing Regist.cools requests, the Formbean used is UserForm, Test.userform class, which returns index.jsp--> when an error occurs in the processing process
<action path= "/regist" type= "test." Registaction "Name=" UserForm "scope=" Request "input="/index.jsp "/>"
<action path= "/overview" forward= "/hello.jsp"/>
<action path= "/failed" forward= "/wuwu.jsp"/>
</action-mappings>
</struts-config>
Third, add a Formbean, the classpath is test. UserForm, here's what this class is about:

Package test;
Import Org.apache.struts.action.ActionForm;
Public class UserForm extends Actionform
{
Private String name= "LPW";//user name
Private String ps= "1111";//Password
Public UserForm () {}
public void SetName (String s) {name=s}
Public String GetName () {return name;}
public void Setps (String s) {ps=s}
Public String Getps () {return PS;}
}


Iv. add a subclass of the action, and the classpath is test. Registaction, here's what this class is about:

Package test;
Import java.lang.reflect.InvocationTargetException;
Import Java.util.Locale;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpSession;
Import Javax.servlet.http.HttpServletResponse;
Import org.apache.struts.action.Action;
Import Org.apache.struts.action.ActionError;
Import org.apache.struts.action.ActionErrors;
Import Org.apache.struts.action.ActionForm;
Import Org.apache.struts.action.ActionForward;
Import org.apache.struts.action.ActionMapping;
Import org.apache.struts.util.MessageResources;
Import Test. UserForm;
Public final class Registaction extends Action
{
Public Actionforward Execute (actionmapping mapping,actionform form, httpservletrequest request, HttpServletResponse Response
Throws Exception
{
Locale Locale = GetLocale (request);
messageresources messages = getresources (request);
HttpSession session = Request.getsession ();
UserForm UserForm = (UserForm) Form;
Here you can invoke other classes to perform database writes or other logical judgments
If UserForm the value of the parameter name is the default LPW, the forward to failed,
The name will go to Struts-config.xml's <global-forwards> for a mapped URL address
(Can be an absolute or relative path), for this example, go to Failed.cool,
Do you remember? Suffix for cool request all into the action-mappings search
The corresponding action processing, the final directory is wuwu.jsp*/
if ("LPW". Equals (Userform.getname ()))
Return (Mapping.findforward ("failed"));
Else
Return (Mapping.findforward ("regist"));
}
}

Five, the following all new or modified pages are equivalent to the view section of struts, the home index.jsp changed to:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK" language= "java"%>
<%@ page import = "test.*"%>
<a href= "Overview.cool" > Site navigation </a><br>
<form action= "Regist.cool" method= "POST" >
<!-the name of the field in the form to be the same as the parameter in UserForm, you can realize the automatic data acquisition function without the need for Request.getparameter ("param");-->
User: <input type= "text" name= "name" ><br>
Password: <input type= "password" name= "PS" ><br>
<input type=submit value= "New User" >
</form>

Vi. add hello.jsp for site navigation:

Vii. add wuwu.jsp, and when no new user is logged in, it will go to this page:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK" language= "java"%>
<jsp:usebean id= "BEANLPW" class= "test." UserForm "scope=" Session "/>
Existing users: <%=beanlpw.getname ()%><br>
Password: <%=beanlpw.getps ()%><br>

No new users!

Eight, add regist.jsp, when a new user login, will go to this page:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK" language= "java"%>
<jsp:usebean id= "BEANLPW" class= "test." UserForm "scope=" Session "/>
New user account: <%=beanlpw.getname ()%><br>
Password: <%=beanlpw.getps ()%>

Nine, start tomcat4, the browser type http://localhost:8080/test/index.jsp , the operation, you can see the results, and a preliminary understanding of struts M, V, c the parts of the cooperative work principle, Of course, this is the author's good will, if the reader read confused, welcome to point out where the error:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.