Entry to struts

Source: Internet
Author: User
Some people may think that struts is not easy to learn. It seems that some of the concepts in it confuse people who have not been familiar with it, such as mvc1, MVC2, and pattern ...... I wrote this article to give people who have never been familiar with Struts a simple Getting Started Guide. Of course, it is necessary to systematically learn struts. There are many fascinating things in it, that's the end.

This case includes the homepage, user logon, and website wizard pages. This is so simple. There is no profound struts concept, mainly relying on hands-on and understanding.

The Web server uses Tomcat 4. Download struts1.1 to http://jakarta.apache.org, release the ZIP file to C:/struts, copy C:/struts/webapps/struts-example.war to C:/tomcat4/webapps, start Tomcat, release the war package as the Struts-example folder, delete the war package, and rename the Struts-example folder as test.
1. Change WEB-INF/Web. XML:

<? XML version = "1.0" encoding = "ISO-8859-1"?>
<! Doctype web-app public "-// Sun Microsystems, Inc. // DTD web application 2.2 //" http://java.sun.com/j2ee/dtds/web-app_2_2.dtd ">

<Web-app>
<! -This is the Controller in struts, the system command is forwarded by it, both the actionservlet class is responsible, it reads the configuration information from the struts-config.xml, and automatically starts a thread in the server background. If there are no special requirements (for example, adding the language editing and conversion function), the programmer can use this part as needed. -->
<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 the cool suffix, rather than a common. jspdo file. The suffix can be changed to any name. Of course, the name must be healthy # ◎ ¥ %! -->
<Servlet-mapping>
<Servlet-Name> action </servlet-Name>
<URL-pattern> *. Cool </url-pattern>
</Servlet-mapping>
<! -- The default homepage of the system is index. jsp, which can be multiple. The system can be searched in order, similar to IIS -->
<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>
</Web-app>

2, the test/WEB-INF/struts-config.xml:

<? 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. In essence, it is a JavaBean, which is used to automatically store the values of various fields in the page form and backfill the form field when appropriate, and does not need to request as traditionally. getparameter ("fieldname");, often used by action in action-mappings -->
<Form-beans>
<! -A new userform class will be added later to store user information. -->
<Form-bean name = "userform" type = "test. userform"/>
</Form-beans>
<! -- This stores the Global Forwarding address that can be used by the entire system, similar to the window in JavaScript. location ('index. JSP '); similar to the buttons on the TV controller, you can switch channels, color, and so on. It is a control flow of Struts-based Web applications. Generally, after an action is processed, it is forwarded to a JSP page for display. This is also the main point of MVC implementation in JSP. -->
<Global-forwards>
<! -- Failed. Cool will be treated as a Servlet request, and the corresponding action will be processed in action-mappings. -->
<Forward name = "failed" Path = "/failed. Cool"/>
<Forward name = "regist" Path = "/regist. jsp"/>
</Global-forwards>
<! -- Do you still remember the request suffixed with cool in Web. xml? They are processed here. This is equivalent to the struts model part, and the model part is more flexible in struts. -->
<Action-mappings>
<! -- Process the regist. cools request. The formbean used is userform, which is a test. userform class. If an error occurs during the processing, index. jsp is returned. -->
<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>
3. Add a formbean with the class path test. userform. The content of this class is as follows:

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 ;}
}

4. Add an action subclass with the class path test. registaction. The following is the content of this class:

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) form;
// Other classes can be called to execute database writing or other logic judgment.
// If the value of the parameter name sent from userform is the default lpw, forward it to the failed,
// This name will go to the struts-config.xml's <global-forwards> to find the mapped URL address
// (It can be an absolute or relative path). In this example, it is switched to failed. Cool,
// Remember? All requests suffixed with cool are searched in action-mappings.
// Corresponding action processing. The final directory is wuwu. jsp */
If ("lpw". Equals (userform. getname ()))
Return (mapping. findforward ("failed "));
Else
Return (mapping. findforward ("regist "));
}
}

5. All the following newly added or modified pages are equivalent to the view section of struts, And the homepage index. jsp is changed:

<% @ 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 field name in the form must be the same as the parameter in userform, so that the data can be automatically obtained without request. getparameter ("Param"). -->
User: <input type = "text" name = "name"> <br>
Password: <input type = "password" name = "Ps"> <br>
<Input type = submit value = "Add User">
</Form>

6. Add hello. jsp for Site Navigation:

<H1> Site Map

7. Add wuwu. jsp. When no new user logs 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 user found!

8. Add regist. jsp. When a new user logs in, it 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 () %>

9. start Tomcat 4 and type http: // localhost: 8080/test/index in the browser. JSP, click here to see the results and get a preliminary understanding of the working principles of M, V, and C in struts. Of course, this is the good intention of the author, if the reader is confused, you are welcome to point out 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.