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
- <? XML version = "1.0" encoding = "UTF-8"?>
- <% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8"
- Pageencoding = "UTF-8" %>
- <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <HTML xmlns = "http://www.w3.org/1999/xhtml">
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
- <Title> insert title here </title>
- </Head>
- <Body>
- <Form action = "login. Action" method = "Post">
- Username: <input type = "text" name = "username"/> <p>
- Password: <input type = "password" name = "password"/> <p>
- <Input type = "Submit" name = "Submit" value = "Submit"/>
- </Form>
- </Body>
- </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
- <? XML version = "1.0" encoding = "UTF-8"?>
- <% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8"
- Pageencoding = "UTF-8" %>
- <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <HTML xmlns = "http://www.w3.org/1999/xhtml">
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
- <Title> insert title here </title>
- </Head>
- <Body>
- Username: $ {requestscope. Username} <br>
- Password: $ {requestscope. Password}
- </Body>
- </Html>
6. modify web. xml
Java code
- <? XML version = "1.0" encoding = "UTF-8"?>
- <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">
- <Display-Name>
- Struts2.0 </display-Name>
- <Welcome-file-List>
- <Welcome-File> login. jsp </welcome-File>
- </Welcome-file-List>
- <Filter>
- <Filter-Name> struts2.0 </filter-Name>
- <Filter-class> org. Apache. struts2.dispatcher. filterdispatcher </filter-class>
- </Filter>
- <Filter-mapping>
- <Filter-Name> struts2.0 </filter-Name>
- <URL-pattern>/* </url-pattern>
- </Filter-mapping>
- </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
- Package com. Action;
- Public class loginaction {
- Private string username;
- Private string password;
- Public String GetUserName (){
- Return username;
- }
- Public void setusername (string username ){
- This. Username = username;
- }
- Public String GetPassword (){
- Return password;
- }
- Public void setpassword (string password ){
- This. Password = password;
- }
- Public String execute () throws exception {
- Return "success ";
- }
- }
Differences between struts1 and struts2:
Struts1 must inherit action, overwrite execute ()
Struts2 is not required
8. Add struts. xml
Java code
- <? XML version = "1.0" encoding = "UTF-8"?>
- <! Doctype struts public
- "-// Apache Software Foundation // DTD struts configuration 2.0 // en"
- Http://struts.apache.org/dtds/struts-2.0.dtd>
- <Struts>
- <Package name = "struts2.0" extends = "struts-Default">
- <Action name = "login" class = "com. Action. loginaction">
- <Result name = "success">/result. jsp </result>
- </Action>
- </Package>
- </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
- <S: Form Action = "login">
- <S: textfield name = "username" label = "username"> </S: textfield>
- <S: password name = "password" label = "password"> </S: Password>
- <S: Submit label = "Submit"> </S: Submit>
- </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
- @ Override
- Public void validate (){
- If (null = This. GetUserName () | "". Equals (this. GetUserName (). Trim ())){
- This. addfielderror ("username", "username error"); // Add an error message
- }
- If (null = This. GetPassword () | "". Equals (this. GetPassword (). Trim ())){
- This. addfielderror ("password", "Password error"); // Add an error message
- }
- }
3. Modify struts. XML to add the page returned by an error
Java code
- <? XML version = "1.0" encoding = "UTF-8"?>
- <! Doctype struts public
- "-// Apache Software Foundation // DTD struts configuration 2.0 // en"
- Http://struts.apache.org/dtds/struts-2.0.dtd>
- <Struts>
- <Package name = "struts2.0" extends = "struts-Default">
- <Action name = "login" class = "com. Action. loginaction">
- <! -- Add the page returned for an error -->
- <Result name = "input">/login. jsp </result> <result name = "success">/result. jsp </result>
- </Action>
- </Package>
- </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
- Public String execute () throws exception {
- If ("hello". Equals (this. GetUserName (). Trim () & "world". Equals (this. GetPassword (). Trim ())){
- Return "success ";
- }
- Else {
- This. addfielderror ("username", "username or password error"); // defines the error message displayed on username.
- Return "failer ";
- }
- }
2. Modify struts. XML to add the failer ing.
Java code
- <? XML version = "1.0" encoding = "UTF-8"?>
- <! Doctype struts public
- "-// Apache Software Foundation // DTD struts configuration 2.0 // en"
- Http://struts.apache.org/dtds/struts-2.0.dtd>
- <Struts>
- <Package name = "struts2.0" extends = "struts-Default">
- <Action name = "login" class = "com. Action. loginaction">
- <Result name = "input">/login. jsp </result>
- <Result name = "success">/result. jsp </result>
- <! -- Added the failer ing -->
- <Result name = "failer">/login. jsp </result>
- </Action>
- </Package>
- </Struts>
3. Complete all. Copy the created web application struts2 to Tomcat to run it.