2. The first struts2 Program-helloworld Program

Source: Internet
Author: User

1. Create a WEB Project: study_struts2

 

2. Create a hellowordaction. Java class

3. Copy the Struts. xml file to the src directory and configure the Struts. xml file as follows:

<?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="aaaa" namespace="/test" extends="struts-default">              <action name="bbbb" class="actions.HelloWordAction">                         <result>/welcome.jsp</result>              </action>              </package></struts>

 

4. Copy web. XML to the WEB-INF directory under webroot, and then configure struts2 startup.

The Web. xml file is configured as follows:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"     xmlns="http://java.sun.com/xml/ns/javaee"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.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>

 

5. The hellowordaction. Java code is as follows:

package actions;public class HelloWordAction {        public String execute(){                System.out.println("I am HelloWordAction.java");        return "success";    }}

 

6. The code for creating welcome. jsp is as follows:

welcome to study Struts2!

 

7. Load struts2 core jar package, the method can be found in the http://www.cnblogs.com/xingyunblog/p/4006150.html

Xwork-core-2.1.6.jar: Core class library of structs2 framework
Struts2-core-2.1.8.1.jar: xwork class library, structs2 built on it
Ognl-2.7.3.jar: object graph Navigation language (structs2) framework reads and writes object attributes through it.
Freemarker-2.3.15.jar: structs2 UI tag template written using freemarker
Commons-logging-1.0.4.jar: the log package produced by ASF, structs2 framework uses this log package to support log4j and jdk1.4 + logging.
Commons-fileupload-1.2.1.jar: File Upload Component, which must be added after version 2.1.6.

8. Release the project and start Tomcat. Enter http: // pc2014092716pel: 8080/study_struts2/test/BBBB in the address bar of the browser.

2. The first struts2 Program-helloworld Program

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.