Struts2.0 Workflow

Source: Internet
Author: User

This is the overall structure of struts 2 provided by struts2 official site.

The process of a request in the struts2 framework is roughly divided into the following steps:

Struts2.0 is a combined version of struts1.0 and webwork2.2. It integrates the advantages of the two popular MVC frameworks and is a major improvement for the Struts framework, it also simplifies the development process of developers to a greater extent.
In this example, a simple login effect is achieved. The procedure is as follows:

1, first of all, to download the full package struts2.0.11 from the Apache website (http://struts.apache.org/downloads.html), extract the need to find the following files:

Commons-logging-1.0.4.jar

Freemarker-2.3.8.jar

Ognl-2.6.11.jar

Struts2-core-2.0.11.jar

Xwork-2.0.4.jar

2. Then, let's start with the first example to create the WEB Project struts2.

3. Copy the JAR file listed above to the struts2 \ WEB-INF \ Lib project, or add your own strust2.0 user Library

4. Create login. jsp

Java code
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8"
  3. Pageencoding = "UTF-8" %>
  4. <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <HTML xmlns = "http://www.w3.org/1999/xhtml">
  6. <Head>
  7. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
  8. <Title> insert title here </title>
  9. </Head>
  10. <Body>
  11. <Form action = "login. Action" method = "Post">
  12. Username: <input type = "text" name = "username"/> <p>
  13. Password: <input type = "password" name = "password"/> <p>
  14. <Input type = "Submit" name = "Submit" value = "Submit"/>
  15. </Form>
  16. </Body>
  17. </Html>



Differences between struts1 and struts2:

<Form action = "login. Action" method = "Post">

Struts1 intercepts all requests sent to end with do through the servlet and provides actionservlet control to determine which action to process.



5. Create result. jsp

Java code
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8"
  3. Pageencoding = "UTF-8" %>
  4. <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <HTML xmlns = "http://www.w3.org/1999/xhtml">
  6. <Head>
  7. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
  8. <Title> insert title here </title>
  9. </Head>
  10. <Body>
  11. Username: $ {requestscope. Username} <br>
  12. Password: $ {requestscope. Password}
  13. </Body>
  14. </Html>

6. modify web. xml

Java code
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Web-app id = "webapp_id" version = "2.4" xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  3. <Display-Name>
  4. Struts2.0 </display-Name>
  5. <Welcome-file-List>
  6. <Welcome-File> login. jsp </welcome-File>
  7. </Welcome-file-List>
  8. <Filter>
  9. <Filter-Name> struts2.0 </filter-Name>
  10. <Filter-class> org. Apache. struts2.dispatcher. filterdispatcher </filter-class>
  11. </Filter>
  12. <Filter-mapping>
  13. <Filter-Name> struts2.0 </filter-Name>
  14. <URL-pattern>/* </url-pattern>
  15. </Filter-mapping>
  16. </Web-app>



Differences between struts1 and struts2:

Struts1 uses Servlet as the controller.

Struts2 uses filter to filter requests sent from the client.



7. Create loginaction. Java

Java code
  1. Package com. Action;
  2. Public class loginaction {
  3. Private string username;
  4. Private string password;
  5. Public String GetUserName (){
  6. Return username;
  7. }
  8. Public void setusername (string username ){
  9. This. Username = username;
  10. }
  11. Public String GetPassword (){
  12. Return password;
  13. }
  14. Public void setpassword (string password ){
  15. This. Password = password;
  16. }
  17. Public String execute () throws exception {
  18. Return "success ";
  19. }
  20. }



Differences between struts1 and struts2:

Struts1 must inherit action, overwrite execute ()

Struts2 is not required



8. Add struts. xml

Java code
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <! Doctype struts public
  3. "-// Apache Software Foundation // DTD struts configuration 2.0 // en"
  4. Http://struts.apache.org/dtds/struts-2.0.dtd>
  5. <Struts>
  6. <Package name = "struts2.0" extends = "struts-Default">
  7. <Action name = "login" class = "com. Action. loginaction">
  8. <Result name = "success">/result. jsp </result>
  9. </Action>
  10. </Package>
  11. </Struts>



Differences between struts1 and struts2:

Struts1 configuration file in project \ WEB-INF \ struts-config.xml

Struts2 in the project \ SRC \ struts. xml configuration file



9. O. A simple example is completed. The following is code refactoring:

Use the struts2.0 tag to add the verification function

1. Modify login. jsp to use the struts2.0 tag.

Import tag <% @ taglib prefix = "S" uri = "/Struts-tags" %>

Replace form with the struts2.0 tag

Java code
  1. <S: Form Action = "login">
  2. <S: textfield name = "username" label = "username"> </S: textfield>
  3. <S: password name = "password" label = "password"> </S: Password>
  4. <S: Submit label = "Submit"> </S: Submit>
  5. </S: Form>

(Labels can be used to automatically wrap lines. The button is right aligned by default)

2. Modify loginaction. Java

Loginaction inherits actionsupport

Override the validate () Verification Method

Java code
  1. @ Override
  2. Public void validate (){
  3. If (null = This. GetUserName () | "". Equals (this. GetUserName (). Trim ())){
  4. This. addfielderror ("username", "username error"); // Add an error message
  5. }
  6. If (null = This. GetPassword () | "". Equals (this. GetPassword (). Trim ())){
  7. This. addfielderror ("password", "Password error"); // Add an error message
  8. }
  9. }

3. Modify struts. XML to add the page returned by an error

Java code
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <! Doctype struts public
  3. "-// Apache Software Foundation // DTD struts configuration 2.0 // en"
  4. Http://struts.apache.org/dtds/struts-2.0.dtd>
  5. <Struts>
  6. <Package name = "struts2.0" extends = "struts-Default">
  7. <Action name = "login" class = "com. Action. loginaction">
  8. <! -- Add the page returned for an error -->
  9. <Result name = "input">/login. jsp </result> <result name = "success">/result. jsp </result>
  10. </Action>
  11. </Package>
  12. </Struts>



Differences between struts1 and struts2:

Struts1 needs to add the <HTML: errors/> label to form to prompt error handling information.

Struts2 is not required



The username is hello and the password is world.

1. Modify the execute () method in loginaction. Java

Java code
  1. Public String execute () throws exception {
  2. If ("hello". Equals (this. GetUserName (). Trim () & "world". Equals (this. GetPassword (). Trim ())){
  3. Return "success ";
  4. }
  5. Else {
  6. This. addfielderror ("username", "username or password error"); // defines the error message displayed on username.
  7. Return "failer ";
  8. }
  9. }

2. Modify struts. XML to add the failer ing.

Java code
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <! Doctype struts public
  3. "-// Apache Software Foundation // DTD struts configuration 2.0 // en"
  4. Http://struts.apache.org/dtds/struts-2.0.dtd>
  5. <Struts>
  6. <Package name = "struts2.0" extends = "struts-Default">
  7. <Action name = "login" class = "com. Action. loginaction">
  8. <Result name = "input">/login. jsp </result>
  9. <Result name = "success">/result. jsp </result>
  10. <! -- Added the failer ing -->
  11. <Result name = "failer">/login. jsp </result>
  12. </Action>
  13. </Package>
  14. </Struts>

3. Complete all. Copy the created web application struts2 to Tomcat to run it.

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.