In fact, struts2 is not an upgraded version of struts 1, but an upgraded version of webwork. Because struts2 is a webwork upgrade, rather than a new framework, it guarantees stability and performance in all aspects: it also absorbs the advantages of struts 1 and webwork, it is a good framework, which is very simple, clean, and powerful. Next we will use struts2 to create a simple logon instance.
After decompression, extract out, the required commons-fileupload-1.2.1.jar, commons-io-1.3.2.jar, freemarker-2.3.15.jar, ognl-2.7.3.jar, struts2-core-2.1.8.1.jar, struts2-pell-multipart-plugin-2.1.8.1.jar, xwork-core-2.1.6.jar, the package copy to the project Lib, and then in the web. add the struts2 filter to XML as follows:
Code:
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Web-app 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>
- <Filter>
- <Filter-Name> struts2 </filter-Name>
- <Filter-class> org. Apache. struts2.dispatcher. Ng. Filter. strutsprepareandexecutefilter </filter-class>
- </Filter>
- <Filter-mapping>
- <Filter-Name> struts2 </filter-Name>
- <URL-pattern>/* </url-pattern>
- </Filter-mapping>
- <Welcome-file-List>
- <Welcome-File> index. jsp </welcome-File>
- </Welcome-file-List>
- </Web-app>
2. Now we can start working. First, write a JSP page.
Code:
- <% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
- <HTML>
- <Head>
- <Title> logon page </title>
- <LINK rel = stylesheet href = "CSS/login.css" type = "text/CSS">
- </Head>
- <Body>
- <H1 align = "center"> User Logon page
- <HR>
- <Div align = "center">
- <Form action = "login. LP" method = "Post">
- <Table cellspacing = 5 Border = 5 bodercolor = # ffaa00>
- <Tr> <TH colspan = "3" align = "center" bgcolor = # ffaa00> User Logon </Th> </tr>
- <Tr>
- <TH rowspan = "3" background = "images/2.jpg" style =" width = 90px "> </Th>
- <TD> User name: </TD> <input type = "text" class = "message" name = "username"> </TD> </tr>
- <Tr> <TD> password: </TD> <input class = "message" type = "password" name = "userpassword"> </TD> </tr>
- <Tr> <TD colspan = "2" align = "center"> <input type = "Submit" value = "Logon"> <input type = "reset" value = "Reset "> </TD> </tr>
- </Table>
- </Form>
- </Div>
- </Body>
- </Html>
3. Write the JSP page, and then write the struts2 action, which is much simpler and more convenient than struts1. As follows:
Code:
- Package myclass. struts2.action;
- Public class loginaction {
- Private string username;
- Private string userpassword;
- Public String GetUserName (){
- Return username;
- }
- Public void setusername (string username ){
- This. Username = username;
- }
- Public String getuserpassword (){
- Return userpassword;
- }
- Public void setuserpassword (string userpassword ){
- This. userpassword = userpassword;
- }
- Public String execute (){
- System. Out. println ("I'm in ");
- System. Out. println (this. GetUserName ());
- System. Out. println (this. getuserpassword ());
- If ("Liping". Equals (this. GetUserName () & "123456". Equals (this. getuserpassword ())){
- Return "succ ";
- } Else {
- Return "fail ";
- }
- }
- }
Also, write an action to exit the system,
Code:
- Package myclass. struts2.action;
- Public class logoutaction {
- Public String execute (){
- System. Out. println ("exit system ");
- Return "exit ";
- }
- }
After writing, you can configure struts. the xml configuration file, which is also the core of struts.1. struts.2 supports separate management of different XML files. Therefore, we configure the login action and logout action separately, as shown below:
Logged on:
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>
- <! -- Use an extension constant to set the request extension to LP. You can set it to any format as needed. -->
- <Constant name = "struts. Action. Extension" value = "LP"/>
- <Package name = "login" namespace = "/" extends = "struts-Default">
- <Action name = "login" class = "myclass. struts2.action. loginaction">
- <Result name = "succ">/succ. jsp </result>
- <Result name = "fail">/failure. jsp </result>
- </Action>
- </Package>
- </Struts>
Logout:
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>
- <Constant name = "struts. Action. Extension" value = "LP"/>
- <Package name = "logout" namespace = "/" extends = "struts-Default">
- <Action name = "logout" class = "myclass. struts2.action. logoutaction">
- <Result name = "exit">/exit. jsp </result>
- </Action>
- </Package>
- </Struts>
The result node is configured with a page jump from action, which matches the value of name. Then put the two XML files into struts:
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>
- <Include file = "login. xml"> </include>
- <Include file = "logout. xml"> </include>
- </Struts>
In this way, the login instance is ready, and then start Tomcat to run!