Struts2中Action的注解范例

struts配置文件
<?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>
<!-- default 在默认的命名空间 -->
<package name="default" extends="struts-default">
<!-- 全局结果定义 -->
<global-results>
<result name="login" type="redirect">/login.action</result>
<result name="sqlException" type="chain">/sqlException.action</result>
<result name="exception">/exception.jsp</result>
</global-results>
<global-exception-mappings>
<!-- 全局异常映射定义 -->
<exception-mapping exception="java.sql.SQLException" result="sqlException"/>
<exception-mapping result="exception" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
<action name="login" class="...">
<!-- 局部异常定义 -->
<exception-mapping exception="rog.coolove.MyException" result="login"/>
<result type="redirectAction">${nextAction}</result>
</action>
</package>
</struts>


import org.apache.struts2.config.Namespace;   
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import org.apache.struts2.dispatcher.ServletDispatcherResult;

import com.opensymphony.xwork2.Action;
@ParentPackage("default")
@Namespace("/test")
@Result("/success.jsp")
@Results({
@Result(name="input", value="/input.jsp"),
@Result(name="error", value="/error.jsp", type=ServletDispatcherResult.class)
})
public class AnnotationAction implements Action
{
@Override
public String execute() throws Exception
{
return SUCCESS;
}

public String input() throws Exception
{
return INPUT;
}

public String error() throws Exception
{
return ERROR;
}
}


web.xml文件
<?xml version="1.0" encoding="UTF-8"?>      
<web-app 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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<!-- 需要配置filter-->
<param-name>actionPackages</param-name>
<param-value>cn.com.sise.expert.web</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值