Use struts2 to create a logon instance

Source: Internet
Author: User

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:
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Web-app version = "2.4"
  3. Xmlns = "http://java.sun.com/xml/ns/j2ee"
  4. Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
  5. Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee
  6. Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
  7. <Filter>
  8. <Filter-Name> struts2 </filter-Name>
  9. <Filter-class> org. Apache. struts2.dispatcher. Ng. Filter. strutsprepareandexecutefilter </filter-class>
  10. </Filter>
  11. <Filter-mapping>
  12. <Filter-Name> struts2 </filter-Name>
  13. <URL-pattern>/* </url-pattern>
  14. </Filter-mapping>
  15. <Welcome-file-List>
  16. <Welcome-File> index. jsp </welcome-File>
  17. </Welcome-file-List>
  18. </Web-app>

2. Now we can start working. First, write a JSP page.

Code:
  1. <% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
  2. <HTML>
  3. <Head>
  4. <Title> logon page </title>
  5. <LINK rel = stylesheet href = "CSS/login.css" type = "text/CSS">
  6. </Head>
  7. <Body>
  8. <H1 align = "center"> User Logon page
  9. <HR>
  10. <Div align = "center">
  11. <Form action = "login. LP" method = "Post">
  12. <Table cellspacing = 5 Border = 5 bodercolor = # ffaa00>
  13. <Tr> <TH colspan = "3" align = "center" bgcolor = # ffaa00> User Logon </Th> </tr>
  14. <Tr>
  15. <TH rowspan = "3" background = "images/2.jpg" style =" width = 90px "> </Th>
  16. <TD> User name: </TD> <input type = "text" class = "message" name = "username"> </TD> </tr>
  17. <Tr> <TD> password: </TD> <input class = "message" type = "password" name = "userpassword"> </TD> </tr>
  18. <Tr> <TD colspan = "2" align = "center"> <input type = "Submit" value = "Logon"> <input type = "reset" value = "Reset "> </TD> </tr>
  19. </Table>
  20. </Form>
  21. </Div>
  22. </Body>
  23. </Html>

3. Write the JSP page, and then write the struts2 action, which is much simpler and more convenient than struts1. As follows:

Code:
  1. Package myclass. struts2.action;
  2. Public class loginaction {
  3. Private string username;
  4. Private string userpassword;
  5. Public String GetUserName (){
  6. Return username;
  7. }
  8. Public void setusername (string username ){
  9. This. Username = username;
  10. }
  11. Public String getuserpassword (){
  12. Return userpassword;
  13. }
  14. Public void setuserpassword (string userpassword ){
  15. This. userpassword = userpassword;
  16. }
  17. Public String execute (){
  18. System. Out. println ("I'm in ");
  19. System. Out. println (this. GetUserName ());
  20. System. Out. println (this. getuserpassword ());
  21. If ("Liping". Equals (this. GetUserName () & "123456". Equals (this. getuserpassword ())){
  22. Return "succ ";
  23. } Else {
  24. Return "fail ";
  25. }
  26. }
  27. }

Also, write an action to exit the system,

Code:
  1. Package myclass. struts2.action;
  2. Public class logoutaction {
  3. Public String execute (){
  4. System. Out. println ("exit system ");
  5. Return "exit ";
  6. }
  7. }

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:
  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. <! -- Use an extension constant to set the request extension to LP. You can set it to any format as needed. -->
  7. <Constant name = "struts. Action. Extension" value = "LP"/>
  8. <Package name = "login" namespace = "/" extends = "struts-Default">
  9. <Action name = "login" class = "myclass. struts2.action. loginaction">
  10. <Result name = "succ">/succ. jsp </result>
  11. <Result name = "fail">/failure. jsp </result>
  12. </Action>
  13. </Package>
  14. </Struts>

Logout:

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. <Constant name = "struts. Action. Extension" value = "LP"/>
  7. <Package name = "logout" namespace = "/" extends = "struts-Default">
  8. <Action name = "logout" class = "myclass. struts2.action. logoutaction">
  9. <Result name = "exit">/exit. jsp </result>
  10. </Action>
  11. </Package>
  12. </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:
  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. <Include file = "login. xml"> </include>
  7. <Include file = "logout. xml"> </include>
  8. </Struts>

In this way, the login instance is ready, and then start Tomcat to run!

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.