Struts2——Action类写法和方法(二)

1.可以自己手动的实现execute()方法

2.实现一个Action的接口

在此接口当中定义了5个常量(逻辑视图)
String SUCCESS = "success";成功

String NONE = "none";没有跳转

String ERROR = "error";失败

String INPUT = "input";表单校验时出错

String LOGIN = "login";登录出错页面跳转
   
示例代码

public class Hello implements Action{
    @Override
    public String execute() throws Exception {
        return ERROR;
    }
}

3.继承Action(推荐使用此方式Actionsupport当中提供了很多功能,数据校验,国际化等一系列操作方法)

public class Hello extends ActionSupport {
    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
}

Method方法访问

通配符访问(在struts.xml添加配置)

第一种常用的:

<action name="goods_*" class="com.Hello" method="{1}">
    <!--对应方法这边一点要注意的就是前面a标签里面写的链接方法和这边的要一致 之前jsp写了大写这边写了小写就没有出来-->
    <allowed-methods>success,login,error,input,none</allowed-methods>
</action>

JSP:

<a href="${pageContext.request.contextPath}/login/goods_success.java">Success</a>
 <a href="${pageContext.request.contextPath}/login/goods_error.java">Error</a>
 <a href="${pageContext.request.contextPath}/login/goods_input.java">Input</a>
 <a href="${pageContext.request.contextPath}/login/goods_login.java">Login</a>
 <a href="${pageContext.request.contextPath}/login/goods_none.java">None</a>

第二种不常用:

<action name="*_*" class="com.{1}" method="{2}">
    <allowed-methods>success,login,error,input,none</allowed-methods>

</action>

jsp:

<a href="${pageContext.request.contextPath}/login/Hello_success.java">Success</a>
<a href="${pageContext.request.contextPath}/login/Hello_error.java">Error</a>
<a href="${pageContext.request.contextPath}/login/Hello_input.java">Input</a>
<a href="${pageContext.request.contextPath}/login/Hello_login.java">Login</a>
<a href="${pageContext.request.contextPath}/login/Hello_none.java">None</a>

第三种动态访问

扫描二维码关注公众号,回复: 4990942 查看本文章
<!--重新改常量 要设置后通配符才能成功-->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="struts2" extends="struts-default" namespace="/login">
<action name="Hello" class="com.Hello">
    <allowed-methods>success,login,error,input,none</allowed-methods>
</action>
</package>

猜你喜欢

转载自blog.csdn.net/qq_40632760/article/details/86546946