struts2实现多个submit方法
我需要在form页面里嵌入多个submit,struts2中参考了很多代码,但是自己测试了下不能实现,不知道有什么好的方法
就是在点击submit,将信息保存在action中,然后,在跳转的页面中能够展现出来,代码如下
package action;
import javax.servlet.http.*;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.*;
public class MoreSubmitAction extends ActionSupport implements
ServletRequestAware
{
private String msg;
private javax.servlet.http.HttpServletRequest request;
public void setServletRequest(HttpServletRequest request)
{
this.request = request;
}
public String save() throws Exception
{
request.setAttribute("result", "成功保存[" + msg + "]");
return "save";
}
public String print() throws Exception
{
request.setAttribute("result", "成功打印[" + msg + "]");
return "print";
}
public String getMsg()
{
return msg;
}
public void setMsg(String msg)
{
this.msg = msg;
}
}
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="demo" extends="struts-default" >
<action name="submit" class="action.MoreSubmitAction">
<result name="save" >
/result.jsp
</result>
<result name="print">
/result.jsp
</result>
</action>
</package>
</struts>
more_submit.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>My JSP 'hello.jsp' starting page</title>
</head>
<body>
<s:form action="submit.action" >
<s:textfield name="msg" label="输入内容"/>
<s:submit name="save" value="保存" align="left" method="save"/>
<s:submit name="print" value="打印" align="left" method="print" />
</s:form>
</body>
</html>
result.jsp
<%@ page pageEncoding="GBK"%>
<html>
<head>
<title>提交结果</title>
</head>
<body>
<h1>${result}</h1>
</body>
</html>
------解决方案--------------------多个submit,你使用js 改变form的action即可。。
表单的action则对应指定的struts2中的 action 方法类
------解决方案--------------------<script>
function test(url){
var form = document.getElementById("myform");
form.action = url;
form.submit();
}
</script>
<form id="myform">
<input type="button" value="保存" onclick="test('你要提交的路径')"/>
<input type="button" value="打印" onclick="test('你要提交的另一个路径')"/>
</form>
------解决方案--------------------以下可以:
<s:submit value="保存" method="save"/>
<s:s