Helloword (1) In struts)

Source: Internet
Author: User


Note: all the images in this article are clearly indicated in the attachment.
Install jdk1.6, Tomcat 6, and myeclipse.

Second, download struts2
Step 1: Go to struts21 official website http://struts.apache.org/2.1.6/index.html
Click download now in Figure 1 below to download the full output in Figure 2. Decompress the package after download;

Step 2;
The integration of myeclipse tomcat is relatively simple, not to mention; create a web project named struts2;
Step 3;
Decompress the following six files in lib under the downloaded struts2-1-6 directory:
Commons-logging-1.0.4.jar
Freemarker-2.3.8.jar
Ognl-2.6.11.jar
Struts2-core-2.0.6.jar
Xwork-2.0.1.jar
And (because it is a struts2-1-6 version. So this file is also essential)
Commons-fileupload-1.2.1

Then paste it to WebRoot/WEB-INF/lib;
Step 4:
Create a login. jsp file in the WebRoot directory.
The Code is as follows:
Login. jsp
Jsp code

  1. <% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>
  2. <%
  3. String path = request. getContextPath ();
  4. String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
  5. %>
  6. My JSP 'login. jsp 'starting page


  7. Step 5:
    Modify the web. xml file under the WEB-INF
    The Code is as follows:
    Web. xml
    Xml Code
    1. Xmlns = "http://java.sun.com/xml/ns/j2ee"
    2. Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
    3. Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee
    4. Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
    5. Struts2
    6. Org. apache. struts2.dispatcher. FilterDispatcher
    7. Struts2
    8. /*


    9. Step 6:
      Create action
      Create the com. test. action package in the src directory.
      Create an action in the package as follows:
      LoginAction. java
      Java code
      1. Package com. test. action;
      2. Public class LoginAction
      3. {
      4. // The getter and setter methods match the client information based on the method name.
      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. {
      19. Return "success ";
      20. }
      21. // Corresponding to
      22. Private String username;
      23. Private String password;
      24. }

        Step 7:
        Struts configuration file

        The following code creates a struts. xml file in the src directory:
        Xml Code
        1. "-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN"
        2. Http://struts.apache.org/dtds/struts-2.0.dtd>
        3. /Result. jsp

        4. Note: The sentence in struts. xml is
          ! DOCTYPE struts PUBLIC
          "-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN"
          Http://struts.apache.org/dtds/struts-2.0.dtd>

          The red sentence may report an error. The solution is to remove the "http: //" text. I don't know what else to do. If you have any knowledge, please give me some advice;

          Step 8;
          Create result File
          The following code creates a result. jsp file in the WebRoot directory:
          Jsp code
          1. ]
          2. <% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>
          3. <%
          4. String path = request. getContextPath ();
          5. String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
          6. %>
          7. My JSP 'result. jsp 'starting page
          8. Helloworld
          9. Username: $ {requestScope. username}
          10. Password: $ {requestScope. password}


          11. So far, the code writing has been completed. Next is release;
            Right-click the name of the struts2 project, select myeclipse from the menu, and select add and remove project ...... Then, figure 3 is displayed.
            Select a project and click add to publish to the specified tomcat,

            Finally, open the browser. In the browser http: // localhost: 8080/struts2/login. jsp

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.