struts2 annotation

声明:我spring也是用的annotation注释的
1.引入依赖的jar包:struts2-convention-plugin.jar
2.在application.xml中加上配置,使得struts与spring的annotation互通
application.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <import resource="classpath*:/platform.xml" />
	<import resource="sodn-server.xml" />
	<context:component-scan base package="xxx.web.action"/>
	<tx:annotation-driven />
</beans>

3.我创建的一个package 继承struts-default,自定义了用户登陆拦截器,和两个全局result。struts.xml如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<package name="default" extends="struts-default"> 
        <!-- 定义一个拦截器  拦截用户必须先登录-->  
        <interceptors>  
            <interceptor name="authority" class="xxx.filter.LoginInterceptor" />  
            <!-- 拦截器栈 -->  
           <interceptor-stack name="mydefault">  
              	<interceptor-ref name="defaultStack" />  
				<interceptor-ref name="authority" />  
            </interceptor-stack>  
        </interceptors>  
  
        <!-- 定义全局Result -->  
        <global-results>  
           <!-- 当返回login视图名时,转入/login.jsp页面 -->  
           <!--<result name="login" >user/login.action</result>  --> 
            <result name="login" >/jsp/login.jsp</result>
            <result name="input" >/jsp/login.jsp</result>
        </global-results>  
	
	</package>	
	<constant name="struts.i18n.encoding" value="UTF-8" />
</struts>

4.常用的struts2 annotation
//声明父包
@ParentPackage("default")
//声明拦截器,多个之间用,隔开
@InterceptorRefs({ 
	@InterceptorRef("mydefault")
})
//声明命名空间,默认为/ 
@Namespace(value="/business")
//声明结果集的路径
@ResultPath("/jsp")
//一个方法对应多个action(一般用于页面跳转)
@Actions({
	@Action(value = "goToOrder", 
		results = { 
			@Result(name = "success", location = "/jsp/business/order.jsp")
		}) ,
	@Action(value = "goToOptical",
		results={
			@Result(name = "success", location = "/jsp/business/optical.jsp")
		}),
	@Action(value = "goToInspect",
		results={
			@Result(name = "success", location = "/jsp/business/inspect.jsp")
		})
})
//单个方法的action,没有返回值
@Action(value = "saveOrder")

你可能感兴趣的:(spring,annotation,struts)