Java Programmer from Stupid Bird to rookie (37) Struts2 (ii) development of the first STRUTS2 instance

Source: Internet
Author: User
Tags constant i18n

This article comes from: Cao Shenhuan blog column. Reprint please indicate the source:http://blog.csdn.net/csh624366188

A previous blog (in detail Struts2 's own implementation STRUTS2 framework) takes you through the MVC business process, and now we use the best framework for MVC STRUTS2 to develop an application instance. Although now MyEclipse8.5 above version has already started to support STRUTS2, but for us to be better familiar with the development struts2 business process, now we still hand to match the environment. First we need to go to struts.apache.org to download the Struts-2.2.3-all package. Now the top version should be 2.3. For a normal use of Struts2, you need at least five packages (depending on the Struts2 version, the package name is slightly different, but the first half of the package name is the same).

Struts2-core-2.0.11.1.jar

Xwork-2.0.4.jar

Commons-logging-1.0.4.jar

Freemarker-2.3.8.jar

Ognl-2.6.11.jar

NOTE: It looks as if some of the older versions also need to add some other jar packs, as shown in the following illustration:

OK, after the jar package joins, we'll start with configuring the environment next. Many students may have such a question, why I submitted the request can go to Struts.xml to find the corresponding action. At least I had a question when I first started to study. Now the answer is for everyone, because the core of the struts2 is the interceptor, and all requests have to go through the interceptor before they are forwarded to the corresponding action. The first intercept request in STRUTS2 is org.apache.struts2.dispatcher.FilterDispatcher this interceptor (next blog we will be analyzing the source of this interceptor), The interceptor does some processing of the request and then goes to the struts.xml to find the corresponding action. Let's take a look at the configuration of Web.xml:


<?xml version= "1.0" encoding= "UTF-8"?> <web-app version=
"2.4" xmlns= "http://java.sun.com/xml/ns/" 
	Java ee " 
	xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " 
	xsi:schemalocation=" http://java.sun.com/ XML/NS/J2EE 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
	<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>

Create struts.xml files in the Server class folder, as required by the official documentation provided by STRUTS2. As the Web project is deployed to the server, when the server is turned on to compile the Web project, the files under the SRC folder are automatically loaded into the Server class folder, so we set up the Struts.xml file directly under SRC, specifically The Struts.xml configuration is 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>
<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 specific meaning:

1.<constant> tags are primarily used to modify Struts.properties profile information, which is equivalent to name and value in struts.properties files, respectively

2.<package> is mainly as a subcontract, such as a project divided into several modules, where each module can be allocated a package, a Struts.xml file can appear multiple <package> tags, here must be extends= " Struts-default "because the core interceptors for struts are all configured in the Struts-default package, without which all requests are not requested to

3. A <action> tag corresponds to an action class, mainly through the action tag to find the class, and then execute the corresponding class. There is a method attribute in the action tag that he can specify in the execution of the action

Let's begin by taking a login as an example to write down the view layer of STRUTS2 development:

login.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<%
String Path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>

OK, the view layer is finished, the following will begin to write the core action, because the business logic layer is to determine whether the user name and password with fixed admin and 123456 equal, so this program is only for testing, it is no longer separate out, It's written directly in the action. Loginaction.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";
	}


As you can see from the above program, we're going to define the data in the form form in the action as a private variable, and then provide the corresponding set and get methods. Many students may have doubts in this. why give him the set and get methods, the data in form forms can be set to the corresponding properties. Why would he default to execute the Execute method? Why is it possible to implement the new setting by modifying the method attribute of the action label in the configuration file? Oh, in this sell in a box, in the next blog, will be all of them to solve these questions.

After the action is written, we can write the struts.xml corresponding, the complete struts.xml of this procedure:

<?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>
<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 Nothing, is to show success and failure of a few words.

OK, here we are, our first STRUTS2 application is finished, let's take a look at the results of the operation:

————》

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.