Struts2 web.xml文件配置

时间:2023-03-09 22:39:15
Struts2 web.xml文件配置

在导入了项目需要使用的核心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;