Struts2 -- Helloworld, struts2.3helloworld

Source: Internet
Author: User

Struts2 -- Helloworld, struts2.3helloworld

When constructing the Struts2 environment, we generally need to do the following steps:

1. Find the jar file to be used to develop the Struts2 application. 2. Create a Web project 3. add Struts2 MVC Framework to xml to start configuration 4. Compile the Struts2 configuration file to develop the jar file on which the Struts2 application depends

You can download struts-2.x.x-all.zipfrom http://struts.apache.org/download.cgi?struts2014. After downloading the file, decompress it. The jar files required for struts2 application development are in the lib folder of the decompressed directory. Different applications require different JAR packages. The following shows the minimum JAR required for developing Struts 2 programs.

Struts2-core-2.x.x.jar: Struts 2 framework core class library

Xwork-core-2.x.x.jar: XWork class library, Struts 2 built on it

Ognl-2.6.x.jar: Object Graph Navigation Language (struts2) framework reads and writes Object attributes through

Freemarker-2.3.x.jar: Struts 2 UI tag template written using FreeMarker

Commons-logging-1.x.x.jar: the log package produced by ASF, Struts 2 framework uses this log package to support Log4J and JDK 1.4 + logging.

Commons-fileupload-1.2.1.jar: File Upload Component, which must be added after version 2.1.6

These Jar files are copied to the WEB-INF/lib directory of the Web project.

Struts2 STARTUP configuration in web. xml

In struts1.x, the struts framework is started through Servlet. In struts2, the struts framework is started through Filter. The configuration in web. xml is as follows:

<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>

In the init () method of StrutsPrepareAndExecuteFilter, the default configuration file struts. xml in the class path is read to complete initialization.

Note: struts2 reads struts. the xml content is stored in the memory in the form of javabean. In the future, struts2 will use the data in the memory for each request processing, instead of reading struts every time. xml file

Configuration file of the Struts2 Application

The default configuration file of Struts2 is struts. xml, which must be stored in the src directory. The configuration template of this file is as follows:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts></struts>
Practice -- each request of helloworldStruts2 must implement the following: Action configuration in JSPActionstruts. xml

1. Create Project 12.01

2. Add the following configuration to the default configuration file struts. xml:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <constant name="struts.devMode" value="true" />    <package name="Hello_World_Struts2" extends="struts-default">        <action name="index">            <result>/index.jsp</result>        </action>        <action name="hello" class="com.wuyudong.helloworld.action.HelloWorldAction" method="execute">            <result name="success">/hello.jsp</result>        </action>    </package></struts>

In the struts2 framework, packages are used to manage actions. packages are similar to class packages in java. They are mainly used to manage actions related to a set of business functions. In practical applications, we should put a group of actions related to business functions under the same package.

When configuring a package, you must specify the name attribute. This attribute value can be named at will, but must be unique. It does not correspond to a java class package. If other packages want to inherit this package, this attribute must be used for reference. The namespace attribute of the package is used to define the namespace of the package. The namespace serves as a part of the path to access the Action under the package. For example, to access the Action in the preceding example, the access path is/test/helloworld. action. The namespace attribute can not be configured. In this example, if this attribute is not specified, the default namespace is "" (empty string ).

Generally, each package should inherit the struts-default package, because many core functions of Struts2 are implemented by the interceptor. For example, encapsulation of request parameters to action, file upload, and data verification in a request is implemented through the interceptor. Struts-default defines the interceptor and Result types. It can be said that the core functions provided by struts2 can be used only when the package inherits struts-default. The struts-default package is defined in the struts2-core-2.x.x.jar in the struts-default.xml file. The struts-default.xml is also the Struts2 default configuration file. Struts2 automatically loads the struts-default.xml file each time.

The com. wuyudong. helloworld. action. HelloWorldAction class used in the example is as follows:

package com.wuyudong.helloworld.action;import com.opensymphony.xwork2.ActionSupport;import com.wuyudong.helloworld.model.MessageStore;public class HelloWorldAction extends ActionSupport {    private static final long serialVersionUID = -4958566543551999157L;    private MessageStore msgStore;    @Override    public String execute() throws Exception {        msgStore = new MessageStore("HelloWorld!");        return SUCCESS;    }    public MessageStore getMsgStore() {        return msgStore;    }    public void setMsgStore(MessageStore msgStore) {        this.msgStore = msgStore;    }}

The hello. jsp file used in the example is as follows:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

In struts2, the URL path for accessing actions in struts2 consists of the package namespace and action name. For example, the URL path for accessing HelloWorldAction in this example is/helloworld (note: complete path: http: // localhost: Port/content path/helloworld ). In addition, you can add the. action suffix to access this Action.

Search Order of Action names

1. Obtain the URI of the Request Path. For example, the url is http: // server/struts2/path1/path2/path3/test. action.

2. first, find the package whose namespace is/path1/path2/path3. If the package does not exist, perform Step 3. If the package exists, search for the action named test in the package, when no action is found in the package, the action is directly searched in the package of the default namaspace (the default namespace is an empty string ""). if the action cannot be found in the package of the default namaspace, the page prompts that the action cannot be found.

3. find the package whose namespace is/path1/path2. If this package does not exist, go to Step 4. If this package exists, search for the action named test in this package, when no action is found in the package, the system will directly go to the package of the default namaspace to find the action named test. The action cannot be found in the package of the default namaspace, the page prompts that the action cannot be found.

4. find the package whose namespace is/path1. If the package does not exist, perform Step 5. If the package exists, search for the action named test in the package, when no action is found in the package, the system will directly go to the package of the default namaspace to find the action named test. The action cannot be found in the package of the default namaspace, the page prompts that the action cannot be found.

5. find the package whose namespace is/. If the package exists, search for the action named test in the package. If the action cannot be found in the package or the package does not exist, action will be searched for in the package of the default namaspace. If the action still cannot be found, the page prompts that the action cannot be found.

Default values in Action configuration

Create Model class MessageStore

package com.wuyudong.helloworld.model;public class MessageStore {    private String message;    public MessageStore(String msg){        this.setMessage(msg);    }    public String getMessage() {        return message;    }    public void setMessage(String message) {        this.message = message;    }}

Create Action class HelloWorldAction, act as Controller

package com.wuyudong.helloworld.action;import com.opensymphony.xwork2.ActionSupport;import com.wuyudong.helloworld.model.MessageStore;public class HelloWorldAction extends ActionSupport {    private static final long serialVersionUID = -4958566543551999157L;    private MessageStore msgStore;    @Override    public String execute() throws Exception {        msgStore = new MessageStore("HelloWorld!");        return SUCCESS;    }    public MessageStore getMsgStore() {        return msgStore;    }    public void setMsgStore(MessageStore msgStore) {        this.msgStore = msgStore;    }}

Configure web. xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>12.01</display-name>   <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>      <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></web-app>

After running

 

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.