Struts2配置及应用详解

本文介绍了Struts2框架的基本配置方法,包括包配置、命名空间配置、包含配置、拦截器配置、常量配置、通配符匹配以及客户端跳转等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

总体概括:Struts2框架中核心组件就是Action、拦截器等,Struts2框架使用包来管理Action和拦截器等。每个包就是多个Action、多个拦截器、多个拦截器引用的集合 

一、我们写一个能运行的Demo,再说详细配置

(1)、在web.xml文件配置struts2默认拦截器

<web-app>
<filter>
  <!-- 配置Struts2核心Filter的名字 -->
        <filter-name>struts2</filter-name>
        <!-- 配置Struts2核心Filter的实现类 -->
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    
 <!-- 配置Filter拦截的URL -->
    <filter-mapping>
    <!-- 配置Struts2的核心FilterDispatcher拦截所有用户请求 -->
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

(2)在src目录下创建struts.xml文件

<package name="basicstruts" extends="struts-default">
	<action name="index">
		<result>index.jsp</result>
	</action>
</package>

(3)在web目录下创建index.jsp

<body>
    hello Struts2
</body>

(4)运行流程

1. 所有的访问都回被web.xml中配置的Struts 的 Filter所拦截<br/>
2. 拦截之后,就进入struts的工作流程<br/>
3. 访问的地址是/index,根据struts按照 struts.xml中的配置,服务端跳转到index.jsp<br/>
4. 显示index.jsp的内容<br/>

二、struts2中几种基本配置

(1)包配置

<struts>
    <package name="default" extends="struts-default">struts2的action必须放在一个指定的包空间下定义
        <action name="login" class="org.qiujy.web.struts.action.LoginAction">定义处理请求URL为login.action的Action
            <result name="success">/success.jsp</result>定义处理结果字符串和资源之间的映射关系
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>

(2)命名空间配置

指定了命名空间/manage,则该包下所有的Action处理的URL应该是“命名空间/Action名”  http://localhost:8080/userlogin_struts2/manage/backLogin.action

(3)包含配置

<struts>
    <include file="struts-default.xml"/>
    <include file="struts-user.xml"/>
    <include file="struts-book.xml"/>
    <include file="struts-shoppingCart.xml"/>
   在Struts2中可以将一个配置文件分解成多个配置文件,那么我们必须在struts.xml中包含其他配置文件
       ......
</struts

(4)拦截器配置(可以简单地看成是Struts中的"filter")

拦截器可以拦截指定的Action,并且对Action进行相应的操作 ,在本例里,拦截了ProductAction,并且注入了当前时间
public class DateInterceptor extends AbstractInterceptor {
 
    public String intercept(ActionInvocation invocation) throws Exception {
       ProductAction action = (ProductAction)invocation.getAction();
       action.setDate(new Date());
       return invocation.invoke();
    }
}
<struts>
    <constant name="struts.i18n.encoding" value="UTF-8"></constant>
 
    <package name="basicstruts" extends="struts-default">
        <interceptors>
            <interceptor name="dateInterceptor" class="com.how2java.interceptor.DateInterceptor" />
        </interceptors>
 
        <action name="*Product*" class="com.how2java.action.ProductAction"
            method="{1}">
            <interceptor-ref name="dateInterceptor" />
            <interceptor-ref name="defaultStack" />   
            <result name="show">show.jsp</result>
            <result name="list">list.jsp</result>
        </action>
 
    </package>
 
</struts>

(5)常亮配置

Struts2框架有两个核心配置文件,其中struts.xml文件主要负责管理应用中的Action映射, 及Action处理结果和物理资源之间的映射关系。除此之外,Struts2框架还包含了一个struts.properties文件,该文件主义了Struts2框架的大量常量属性。但通常推荐也是在struts.xml文件中来配置这些常量属性。
如:后面会讲到Struts2的国际化,它的资源文件位置就用常量属性来指定:
<struts>
    ......
    <constant name="struts.custom.i18n.resources" value="messages"/>
</struts>

(6)通配符匹配

*Product 表示所有以Product结尾的路径,都会匹配在这个Action上 
method={1}表示第一个通配符 
如果访问的是addProduct就会调用add方法 
如果访问的是listProduct就会调用list方法 
以此类推
<action name="*Product" class="com.tcy.action.ProductAction" method="{1}">
    <result name="show">show.jsp</result>
    <result name="list">list.jsp</result>
</action>

(7)客户端跳转

public String addPage(){
        return "addPage";
}
//传递参数
public String addPage(){
        name = "default name";
        return "addPage";
}
<action name="addPageProduct" class="com.tcy.action.ProductAction" method="addPage">
    <result name="addPage" type="redirect">addProduct.jsp</result>
</action>
<result name="addPage" type="redirect">addProduct.jsp?name=${name}</result>

(8)struts注解配置

@Namespace("/")
@ParentPackage("struts-default")
@Results({@Result(name="show", location="/show.jsp"),@Result(name="home", location="/index.jsp")})
public class ProductAction {
    private Product product;
    @Action("showProduct")
    public String show() {
        product = new Product();
        product.setName("iphone7");
        return "show";
    }
 
    public Product getProduct() {
        return product;
    }
 
    public void setProduct(Product product) {
        this.product = product;
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值