Java programmers go from stupid birds to cainiao () to talk about struts2 (2) Developing the first struts2 instance

Source: Internet
Author: User
Tags i18n

This article is from: Cao shenghuan blog column. Reprinted please indicate the source:Http://


Previous blog (Let's talk about struts2's self-implemented struts2 framework) To familiarize you with the MVC-based business process. Now we use struts2, the best implementation framework for MVC, to develop an application instance. Although myeclipse8.5 and later versions have already started to support struts2, in order to better familiarize ourselves with the business process of developing struts2, we need to work with the Environment manually. First, we need to go to to download the Struts-2.2.3-All package. The maximum version should be 2.3. To use struts2 normally, at least five packages are required (the package names may vary slightly because of the different versions of struts2, but the first half of the package names are the same ).






Note: It seems that some later versions still need to add some other jar packages, as shown in:

Now, after adding the jar package, we will start to work with the configuration environment. Many may have such questions: why can I find the corresponding action in struts. xml when I submit a request ?? At least I had such a question when I first started learning. Now we can reveal the answer, because the core of struts2 is the interceptor, and all requests must be forwarded to the corresponding action through the interceptor. The first request intercepted in struts2 is the interceptor org. Apache. struts2.dispatcher. filterdispatcher.(In the next blog, we will analyze the source code of the Interceptor.) The interceptor processes the request and then searches for the corresponding action in struts. xml. Let's take a look at the web. xml configuration:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation=""><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>*.action</url-pattern></filter-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

In the official documentation provided by struts2, create a struts. xml file in the class folder of the server. Because the WEB Project is deployed to the server, when the server is enabled to compile the WEB Project, the files in the SRC folder are automatically loaded to the class folder of the server, therefore, we directly create struts under SRC. XML file,DetailsStruts. xmlConfigurationAs follows:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    ""><struts><constant name="struts.i18n.encoding" value="utf-8" /><package name="struts2" extends="struts-default"><action name="" class=""><result name=""></result><result name=""></result></action></package></struts>   

Note: The above Code has the following meanings:

1. The <constant> label is mainly used to modify the Struts. properties configuration file information. The name and value are equivalent to the name and value in the Struts. properties file respectively.

2. <package> it is mainly used as a subcontract. For example, a project is divided into several modules. Here, one package and one struts can be allocated to each module. multiple <package> labels can appear in the XML file. Here, extends = "struts-Default" must be available, because the core interceptor of Struts is configured in the Struts-default package, without this, all requests will not be sent

3. A <action> label corresponds to an action class. It mainly searches for the class in the Action label and then executes the corresponding class. The action tag has a method attribute, which specifies the method in which the action is executed.

Next, we will take logon as an example to write the view layer developed by struts2:

Login. jsp

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% string Path = request. getcontextpath (); string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; %> <HTML> 

Well, the view layer is finished, and the core action will be written below. Because the business logic layer is to determine whether the user name and password are equal to the fixed admin and 123456, this program is only for testing, the loginaction is directly written in the action. java

package com.bzu.action;public class LoginAction {private String username;private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String execute(){if(username.equals("admin")&&password.equals("123456"))return "success";return "fail";}}

From the above procedure, we can see that in the action, we need to define the data in the form of private variables, and then provide the corresponding set and get methods. Many may have questions here.Why is the set and get methods provided to him, so that the data in the form can be set to the corresponding attribute? Why does it execute the execute method by default? Why can I execute the new setting method after modifying the method attribute corresponding to the action tag in the configuration file?Haha, I am selling a token here. In the next blog, I will solve these questions one by one.

After the action is completed, we can write the corresponding struts. XML, the complete struts. xml of this program:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    ""><struts><constant name="struts.i18n.encoding" value="utf-8" /><package name="struts2" extends="struts-default"><action name="LoginAction" class="com.bzu.action.LoginAction"><result name="success">success.jsp</result><result name="fail">fail.jsp</result></action></package></struts>    


The corresponding success. jsp and Fai. jsp have no content, that is, display the words "success" and "failure.

Now, the first struts2 application is finished. Let's take a look at the running results:



Related Article

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