Document directory
- Configure web. XML in WEB-INF to add struts2 framework to Web application;
- Configure struts. XML in src (eclipse will copy all files except java files under the src directory to the WEB-INF \ classes at compilation)
- Import struts core class library into WEB-INF \ Lib;
Environment of this article:
1. eclipse for javaee developer Helios
2. Struts 2.3.1.1
3. Tomcat 7.0.6
Prerequisites: tomcat configuration is complete. This document omits the tomcat configuration steps.
In fact, the configuration process of myeclipse and eclipse for javaee is similar. The only difference is:
Eclipse for javaee creates dynamic Web projec;
Myeclipse creates a web project;
1. Create a dynamic web project
2. click Next.
3. See the Output Folder is build \ classes, and the traditional WEB-INF \ Classes are different, but do not need to pay attention to the development;
4.
Configure the web. XML. To add the struts2 framework to a web application, configure struts in SRC. XML (eclipse will copy all the files except the java files under the src directory to the WEB-INF \ classes at compilation) to import the struts core class library into the WEB-INF \ Lib;
5. compile web. the purpose of xml configuration here is to integrate the struts2 framework into a web application. Here, a filter is configured. We can learn from the previous steps that the filter is used to complete some tasks before the servlet is executed, from the <URL-pattern>, we can see that any request will enter the scope of the struts2 framework;
<?xml version="1.0" encoding="GBK"?><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" version="3.0"> <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>
6. Compile struts. xml
<?xml version="1.0" encoding="GBK" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> </struts>
Write Hello World
1. Create a hello. jsp file with the content Hello struts2 !!!
2. Configure struts. xml
<struts><constant name="struts.devMode" value="true"></constant> <package name="HelloPackage" namespace="/" extends="struts-default"> <action name="Hello"> <result>/Hello.jsp</result> </action> </package></struts>
3. Deploy and enter http: // localhost: 8888/strutsdemo01/hello in the browser
Note: in future development, you must add the following content to the <struts> element:
<Constant name = "struts. devmode" value = "true"> </constant>
This indicates that the developer mode is used to provide more prompt information when an error occurs;