Struts 2 Simple configuration analysis

Source: Internet
Author: User
Tags i18n

To configure Struts 2, first to have a Struts 2 jar package, you can go to Struts's official website download (http://struts.apache.org/), there are 3 GA version can choose to download, I chose the latest 2.2.3.1 version.

If your hard disk space is larger, it is recommended to download the Struts-2.2.3.1-all.zip version as it contains the example (very useful) and many documents that Struts 2 gives us. After downloading the struts-2.2.3.1-all.zip, unzip it to a folder, after the decompression looks like:

Under the Apps folder, there are examples of struts 2 available to us that can be run directly, in which there are plenty of configuration information we need (especially for friends who are unfamiliar with configuration information).

Docs folder, there are some documentation instructions for Struts 2.

Lib folder, the class library files and dependent library files needed to develop struts 2

src folder, for the source code of Struts 2, you can use the IDE to include this folder, so you can see the source of Struts 2 in the IDE?

??

Here's a simple example of how to configure Struts 2 simply to get it up and running:

The program's approximate flow is: The user enters a name, and Struts 2 returns a welcome page.

First, create a Java Web project, I'm using (MyEclipse 9.0 development environment), and I'll name the project hello.

In the second step, we import the Struts 2 package into the project, do not import all the packages inside the LIB package into the project (about 77 jar files ...). That would allow struts 2 to run, but it contains a lot of packages that are not available at this stage, and the best thing to do is to import the packages that are most needed at this stage, because my example is simple and does not require advanced functionality, so you only need to import struts 2 to support the smallest package. Perhaps the smallest package introduced by many tutorials or books is not the same as the one we downloaded, so how do we know the minimum package of a version? The items under the Apps folder can be very useful.

See the Apps folder with 5 items, select "Struts2-blank.war" project, the project name blank means empty project, just can let struts run up the smallest configuration, with decompression software (winrar, etc.) extracted

Open the "Web-inf" folder under the Unzip folder:

Open the Lib folder, which contains a package that supports minimum configuration struts 2, imports the package into your project, and you can use the basic Struts 2 feature.

The third step is to find the "Web. xml" file in your project, configure it so that the user sends a request to the server to be intercepted by the Struts 2 framework

For a friend who is unfamiliar with the profile, may not know how to configure, this time we can still not open the books and search for information on the Internet, just to opened the project we just unzipped, in the "Struts2-blank.war" project to find the Web. xml file, open it can see

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app id= "Webapp_9" version= "2.4"
Xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"
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" >

<display-name>struts blank</display-name>

<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.html</welcome-file>
</welcome-file-list>

</web-app>

This is the information in our project that the Web. xml file needs to be configured, you can copy the past directly, completely no problem, we ligatures code time is saved (-_-!! I'm lazy, but the advice is to knock a few more times. The following explains the meaning of these tags, the filter tag, is used to define a filter, Filter-name is the name of the filter, can be arbitrary, filter-class for the filter to use what class implementation. The filter-mapping tag, which is the map of the filter, Filter-name indicates which filter to use, Url-pattern indicates what type of request to filter, and "/*" represents all requests. Display-name tags and welcome-file-list tags have no big impact, and you can also use them. These tags are combined to represent full-text information: All requests sent by the browser to the filter named "Struts2" processing, This filter is implemented by the Org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter class, which implements the context in which the Struts 2 framework takes over the Web request.

Fourth step, create a form to receive the data:

<s:form action= "Welcome" >
<s:textfield name= "username" label= "Enter Your Name" ></s:textfield>
<s:submit value= "Submit" ></s:submit>
</s:form>

Note that when using the STRUTS2 tag Library, please add the import to the STRUTS2 tag library on the page

<%@ taglib prefix= "s" uri= "/struts-tags"%>

The fifth step is to create the action class that receives the data:

public class Welcomeaction extends Actionsupport
{
Private static final long serialversionuid = -6256745897215180249l;

Private String username;

Public String GetUserName ()
{
return username;
}

public void Setusername (String username)
{
This.username = Username;
}

@Override
Public String Execute ()
{
return SUCCESS;
}

}

The sixth step is to create a page that receives the data successfully:

<body>
Welcome,<s:property value= "username"/>
</body>

STRUTS2 's tag library uses a powerful object map navigation language (OGNL), here just to show a greeting page, inside the logic of a lot of content, there is a chance I will summarize the OGNL knowledge points (of course, my level is limited, perhaps the expression of the poor or not comprehensive, then please understand, we learn together).

Seventh step, create the Struts.xml configuration file

Also encountered unfamiliar configuration file, this time said can be lazy:), open the "Struts2-blank.war" project under the Web-inf/classes folder named "Struts.xml" file:

<?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.enable.DynamicMethodInvocation" value= "false"/>
<constant name= "Struts.devmode" value= "false"/>

<package name= "Default" namespace= "/" extends= "Struts-default" >

<default-action-ref name= "index"/>

<global-results>
<result name= "Error" >/error.jsp</result>
</global-results>

<global-exception-mappings>
<exception-mapping exception= "java.lang.Exception" result= "error"/>
</global-exception-mappings>

<action name= "Index" >
<result type= "Redirectaction" >
<param name= "ActionName" >HelloWorld</param>
<param name= "namespace" >/example</param>
</result>
</action>
</package>

<include file= "Example.xml"/>

<!--ADD packages here--

</struts>

This time, you can't copy it directly. Using the-_-!!, what we need most is the declaration of the DTD file at the head of the document, because he is really difficult to remember, you can save it in an easy to access place, the next development will not have to look for it. The other parts of the document roughly show the general look of the Struts.xml profile, and you can refer to a number of tags that we don't use this time. Perhaps you will wonder why the label inside the name, why not to call other names, why I know I was also very confused, those who first developed the people how to know, the back of a senior told me, in fact, the DTD file can be downloaded to see, it shows what the label can be used, How to use it. Is the URL of the Declaration field (HTTP://STRUTS.APACHE.ORG/DTDS/STRUTS-2.0.DTD). Download it down roughly like this:

View Code

Looking at this document requires a certain regular expression of the basis, interested can go to see.

After understanding the above knowledge, create our own Struts.xml file, and note that the file needs to be created in the SRC directory (using the IDE), otherwise it needs to be created in the Web-inf/classes directory.

<?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= "Welcomepackage" extends= "Struts-default" >
<action name= "Welcome" class= "Struts2.suxiaolei.action.WelcomeAction" >
<result name= "Success" >/welcome_success.jsp</result>
<result name= "Input" >/welcome.jsp</result>
</action>
</package>
</struts>

The package element, which acts like a mechanism for Java packages, is a tool for categorizing, extends attributes, like his name, inherits all the information from Struts-default, and it is generally best to inherit it by creating a package of our own. Because he gives us most of the functionality, you can find this package in the Struts-default.xml file in the Struts2-core jar package. The action element corresponds to your form, such as the action= "Welcome" of your form, and the form submits the parameter to the action's name= "Welcome" implementation class. The result element is the outcome of the action, which is selected by the control field returned by the action class.

Finally, the test project

In the browser, enter: http://localhost:8081/hello/welcome.jsp, get the following interface

Enter a name of "Tom", click "Submit" button, the result is as follows:

You can see that Struts 2 has run successfully.

There is a point, if the submission is Chinese how to do, the following first see what will be the effect:

You can see the Chinese become a string of characters to understand, to solve this problem can be seen under the ORG.APACHE.STRUTS2 package of the Default.properties file, the default property file has a line representing the internationalization of the properties

# # # This can is used to set your default locale and encoding scheme
# Struts.locale=en_us
Struts.i18n.encoding=utf-8

You can add it under the Package tab in your Struts.xml file:

<constant name= "struts.i18n.encoding" value= "GBK" ></constant>

Restart the server, browser input: http://localhost:8081/hello/welcome.jsp

IE Effect:

Firefox effect:

Opera effect:

It seems that this method only, IE will have effect-_-!!

The original also to the page encoding mode to GBK, the default way is "Iso-8859-1", this does not support Chinese

<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%>

Look at the effect

It's finally done! The original coding problem is also so troublesome ... , the statement is not good please forgive me, if there is a mistake welcome point.

Struts 2 Simple configuration analysis

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.