在導入了項目需要使用的核心jar包之后需要在web.xml中配置Struts。
1. Struts2的知識點普及:
Struts2共有5類配置文件,分別羅列如下:
1), Web.xml;
在沒有使用框架的時候,以前額JSP,Servlet程序中就配置過web.xml文件。准確地說web.xml不屬於Struts2框架特有的配置文件。作為部署文件,web.xml是所有Java Web項目的核心文件。然而在這里之所以配置該文件。是因為在web項目中使用struts 2 框架時,還需要在web.xml中配置struts2的核心控制器(StrutsPrepareAndExecuteFilter),用於對struts2框架進行初始化和處理所有的請求。
具體配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>StruteDemo</display-name> <welcome-file-list> <welcome-file>default.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>
2), struts.properties;
3), struts.xml;
struts.xml定義應用自身使用的action映射及result,但我們一般將應用的各個模塊分到不同的配置文件中。
首先,必須在struuts.xml文件的對應程序中做相應的配置,其中需要對每一個動作進行相應攔截器的調用,對每一種動作的運行結果進行配置等。在攔截器中,必須在使用前進行注冊,struts配置文件可以支持繼承,默認的配置文件包在Struts 2-core-x.x.x.jar中
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results> <result name="error">/WEB-INF/jsp/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>
4), struts-plugin.xml;
5), struts_default.xml;