structs2的web.xml和struct.xml两个配置文件的配置

来源:互联网 发布:业务数据运维管理规范 编辑:程序博客网 时间:2024/05/29 19:19

            上篇博文提到了structs2的下载和使用。本文将简介一下两个主要的配置文件web.xml和struct.xml.


1.web.xml文件

<?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>

这个文件基本在示例代码中就配置完成,不需要修改。



2.struct.xml

<?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">        <action name="release" class="com.testService.action.releaseAction" method="restore" >              <result >/Hello.jsp</result>          </action>              </package>    <include file="example.xml"/>    <!-- Add packages here --></struts>

            这个配置文件就需要重点修改。首先注意package标签里的namespace属性,类似C++里的namespace,相当于修改了相对地址的基准地址,在下面的跳转文件里响应地都要加上这个基准地址。

            然后就是写action跳转。前台访问后台的url地址末尾都会加上action的name属性,例如上面的action访问地址为:http://localhost:8080/testService/release

            action标签的class属性标明action的具体Java类文件的之地址,包名+文件名

            action标签的method属性标明action类中首先调用的方法名,上面就是restore方法。如果缺省method就直接会调用execute方法.

            最后那个result标签就是返回的页面。


0 0
原创粉丝点击