Struts2可以实现在同一个action中通过多个方法实现在配置文件中配置出多个逻辑的action,通过在js中给各个button添加onclick方法调用实现提交到不同的action。可以实现多个按钮的动态提交。并且可以通过在action中设置参数属性在struts.xml文件中的result中配置表达式来动态跳转到不同的逻辑视图
package com.inspur.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class MyAction extends ActionSupport {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String target;
private String tip;
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
public String login(){
if(this.getUsername().equals("lzhq")&&this.getPassword().equals("123")){
ActionContext actionContext=ActionContext.getContext();
Map session=actionContext.getSession();
session.put("user", this.getUsername());
return SUCCESS;
}else
return ERROR;
}
public String direct(){
if(this.getTarget().equals("welcome"))
return SUCCESS;
else
return ERROR;
}
}
在struts.xml文件中通过属性 值在result中配置表达式实现动态跳转
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="demo2" extends="struts-default" namespace="/">
<action name="login" class="com.inspur.action.MyAction" method="login">
<result name="success">/success.jsp</result>
<result name="error">/loginFail.jsp</result>
</action>
<action name="direct" class="com.inspur.action.MyAction" method="direct">
<result name="success">/${target}.jsp</result>
<result name="error">/directFault.jsp</result>
</action>
</package>
</struts>