Steps to add Struts2 support for Java Web projects __HTML5

Source: Internet
Author: User
Tags java web

Starting today to learn Struts2, STRUTS2 uses the MVC design pattern to make Java Web applications more hierarchical, a very popular framework, and I record several steps to add STRUTS2 support in a Java Web application:

First, create a new common Java Web application in MyEclipse


Here we name the project Struts2app, the project structure as shown above

Add Struts2-dependent jar packages to the Lib folder

In fact, MyEclipse can automatically add struts support to the Web application we create, but here I am not going to use the MyEclipse feature, but I will manually add the Struts2 to the project, the imported jar packages are mainly as follows:


The jar pack above can be obtained from http://www.apache.org/'s official web site.

Iii. Preparation of Web.xml documents

By default, the Web.xml file in the newly created Java Web project is like this:

<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "3.0" 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/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">
	<display-name></ display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	< /welcome-file-list>
</web-app>
Web.xml file in the project's/webroot/web-inf path, we need to edit the file, add a filter in the file, configure the filter, make the URL request to struts to process, the edited Web.xml file code is as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "3.0" 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/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">
	<display-name></ 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>
We have added the filter and filter-mapping tags to the web.xml file, which sets the STRUTS2 filter, which specifies the filter URL type/*, which filters all types of URLs

Iv. Preparation of Struts.xml documents

The Struts.xml file is the core file in the STRUTS2 framework, all filtered requests are processed by the Struts.xml file, and we create a new XML file in the SRC directory of the project, named Struts.xml, 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=" Example "extends=" Struts-default "> 
		<action name=" Login "class=" com.test.action.LoginAction ">
			<result name=" OK " >/ok.jsp</result>
		</action>
	</package>
</struts>
The configuration file above specifies package and action, result, and so on, package similar to the package in Java engineering, but the above package inherits the Struts-default package, and then there is an action in the package named login, The corresponding class is com.test.action.LoginAction, which means that the request named login will be given to the Com.test.action.LoginAction class to handle, this loginaction class, is an ordinary Java class, not following For any class, does not implement any interface, its code is as follows:

Package com.test.action;

public class Loginaction {public

	String execute () throws exception{return
		"OK";
	}

As you can see, the code in the Loginaction class is just one method: Execute (), which returns the string type, where the return value corresponds to the result label in the above Struts.xml file, that is, when Loginaction returns "OK", OK in the Struts.xml file will find the corresponding JSP page that ok.jsp, and then the request will be forwarded to ok.jsp

V. Writing ok.jsp pages

VI. Test run

We deploy the project to Tomcat, enter it in the browser: Http://localhost:8080/Struts2App/login, the content that appears after the visit is the ok.jsp page




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.