Build and test the struts2 Environment

Source: Internet
Author: User

Resource Package download: http://download.csdn.net/source/830515

The configuration process is as follows:
1. Copy commons-logging-1.1.jar freemarker-2.3.8.jar ognl-2.6.11.jar struts2-core-2.0.6.jar to lib
2. Modify web. xml

  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. <! -- Define the filter of filterdispathcer of struts2 -->
  8. <Filter>
  9. <Filter-Name> struts2 </filter-Name>
  10. <Filter-class> org. Apache. struts2.dispatcher. filterdispatcher </filter-class>
  11. </Filter>
  12. <! -- Filterdispatcher is used to initialize struts2 and process all Web requests. -->
  13. <Filter-mapping>
  14. <Filter-Name> struts2 </filter-Name>
  15. <URL-pattern>/* </url-pattern>
  16. </Filter-mapping>
  17. </Web-app>

Struts. xml

  1. <? XML version = "1.0" encoding = "GBK"?>
  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 = "jcuckoo" extends = "struts-Default">
  7. <Action name = "login" class = "jcuckoo. loginaction">
  8. <Result name = "input">/login. jsp </result>
  9. <Result name = "error">/error. jsp </result>
  10. <Result name = "success">/welcome. jsp </result>
  11. </Action>
  12. </Package>
  13. </Struts>

Loginaction

  1. Public class loginaction extends actionsupport {
  2. Private string username;
  3. Private string password;
  4. Public String GetUserName (){
  5. Return username;
  6. }
  7. Public void setusername (string username ){
  8. This. Username = username;
  9. }
  10. Public String GetPassword (){
  11. Return password;
  12. }
  13. Public void setpassword (string password ){
  14. This. Password = password;
  15. }
  16. Public String execute () throws exception {
  17. If (GetUserName (). Equals ("admin") & GetPassword (). Equals ("admin ")){
  18. Actioncontext. getcontext (). getsession (). Put ("user", GetUserName ());
  19. Return success;
  20. } Else {
  21. Return Error;
  22. }
  23. }
  24. @ Override
  25. Public void validate (){
  26. If (GetUserName () = NULL | GetUserName (). Trim (). Equals ("")){
  27. Addfielderror ("username", gettext ("user. Required "));
  28. }
  29. If (GetPassword () = NULL | GetPassword (). Trim (). Equals ("")){
  30. Addfielderror ("password", gettext ("pass. Required "));
  31. }
  32. }
  33. }

Messageresource. Properties

  1. Loginpage = logon page
  2. Errorpage = Error Page
  3. Succpage = Success page
  4. Failtip = sorry, you cannot log on!
  5. Succtip = welcome, {0}. You have logged on!
  6. Viewlink = view the books published by Li Gang
  7. Bookpagetitle = published by Li Gang
  8. Bookname = title:
  9. User = User Name
  10. Pass = Password
  11. Login = Login
  12. User. Required = the user name is required!
  13. Pass. Required = the password is required!

Login. jsp

  1. <% @ Page Language = "Java" contenttype = "text/html; charset = GBK" %>
  2. <% @ Taglib prefix = "S" uri = "/Struts-tags" %>
  3. <HTML>
  4. <Head>
  5. <Title> <s: Text name = "loginpage"/>
  6. </Title>
  7. </Head>
  8. <Body>
  9. <S: Form Action = "login">
  10. <S: textfield name = "username" Key = "user"/>
  11. <S: textfield name = "password" Key = "pass"/>
  12. <S: Submit key = "login"/>
  13. </S: Form>
  14. </Body>
  15. </Html>

Error. jsp

  1. <% @ Page Language = "Java" contenttype = "text/html; charset = GBK" %>
  2. <% @ Taglib prefix = "S" uri = "/Struts-tags" %>
  3. <HTML>
  4. <Head>
  5. <Title> <s: Text name = "errorpage"/> </title>
  6. </Head>
  7. <Body>
  8. <S: Text name = "failtip"/>
  9. </Body>
  10. </Html>

Welcome. jsp

  1. <% @ Page Language = "Java" contenttype = "text/html; charset = GBK" %>
  2. <% @ Taglib prefix = "S" uri = "/Struts-tags" %>
  3. <HTML>
  4. <Head>
  5. <Title> <s: Text name = "succpage"/> </title>
  6. </Head>
  7. <Body>
  8. <S: Text name = "succtip">
  9. <S: Param >$ {sessionscope. User} </S: param>
  10. </S: Text> <br>
  11. <A href = "getbooks. Action"> <s: Text name = "viewlink"/> </a>
  12. </Body>
  13. </Html>




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.