struts2 多个提交按钮问题
我看来一个例子,是struts2(2.1.8)可以处理2个或多个提交按钮!但是我照着例子做,却不对!麻烦哪位大哥(大姐)指点一下!下边的是代码!submit.jsp<%@...
我看来一个例子,是struts2(2.1.8)可以处理2个或多个提交按钮!但是我照着例子做,却不对!麻烦哪位大哥(大姐)指点一下!下边的是代码!
submit.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>多个submit</title>
</head>
<body>
<form name="form1" action="submit">
<input name="message" type="text" />
<input name="save" type="submit" value="保存" method="save"/>
<input name="print" type="submit" value="打印" method="print"/>
</form>
</body>
</html>
struts.xml
<?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="submit" extends="struts-default">
<action name="a" class="submit.Submit">
<result name="save">save.jsp</result>
<result name="print">print.jsp</result>
</action>
</package>
</struts>
Submit.java
package submit;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class Submit extends ActionSupport implements ServletRequestAware {
private static final long serialVersionUID = -2690112607332622329L;
private String message;
private HttpServletRequest request;
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
public String save(){
request.setAttribute("result", message);
return "save";
}
public String print(){
request.setAttribute("result", message);
return "print";
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
这个在submit包中!
单击打印或是保存按钮,会报找不到result,我试了试,返回的是success,而且不执行save和print这两个方法!希望告诉赐教!小弟谢谢了!
struts.xml文件中打错了,<action name="submit" class="submit.Submit">应该是这样!希望高手帮助!
1楼的大哥说的是对的!但是能说明一下为什么必须要用struts2的标签吗?小弟先谢了!呵呵! 展开
submit.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>多个submit</title>
</head>
<body>
<form name="form1" action="submit">
<input name="message" type="text" />
<input name="save" type="submit" value="保存" method="save"/>
<input name="print" type="submit" value="打印" method="print"/>
</form>
</body>
</html>
struts.xml
<?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="submit" extends="struts-default">
<action name="a" class="submit.Submit">
<result name="save">save.jsp</result>
<result name="print">print.jsp</result>
</action>
</package>
</struts>
Submit.java
package submit;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class Submit extends ActionSupport implements ServletRequestAware {
private static final long serialVersionUID = -2690112607332622329L;
private String message;
private HttpServletRequest request;
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
public String save(){
request.setAttribute("result", message);
return "save";
}
public String print(){
request.setAttribute("result", message);
return "print";
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
这个在submit包中!
单击打印或是保存按钮,会报找不到result,我试了试,返回的是success,而且不执行save和print这两个方法!希望告诉赐教!小弟谢谢了!
struts.xml文件中打错了,<action name="submit" class="submit.Submit">应该是这样!希望高手帮助!
1楼的大哥说的是对的!但是能说明一下为什么必须要用struts2的标签吗?小弟先谢了!呵呵! 展开
3个回答
展开全部
你的思想和action方法都没有错,错误时因为你页面上没有使用struts2的标签。将你的submit.jsp改成下面就可以了。
<%@ page language="java" pageEncoding="utf-8"%>
<!-- 导入struts2的标签库 -->
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>多个submit</title>
</head>
<body>
<s:form name="form1" action="submit" method="post">
<input name="message" type="text" /><br>
<s:submit name="save" method="save" value="保存"></s:submit>
<s:submit name="print" method="print" value="打印"></s:submit>
</s:form>
</body>
</html>
其实,主要是这2个按钮一定要用struts2的标签
<s:submit name="save" method="save" value="保存"></s:submit>
<s:submit name="print" method="print" value="打印"></s:submit>
<%@ page language="java" pageEncoding="utf-8"%>
<!-- 导入struts2的标签库 -->
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>多个submit</title>
</head>
<body>
<s:form name="form1" action="submit" method="post">
<input name="message" type="text" /><br>
<s:submit name="save" method="save" value="保存"></s:submit>
<s:submit name="print" method="print" value="打印"></s:submit>
</s:form>
</body>
</html>
其实,主要是这2个按钮一定要用struts2的标签
<s:submit name="save" method="save" value="保存"></s:submit>
<s:submit name="print" method="print" value="打印"></s:submit>
展开全部
一楼的为什么不可以用
<input name="save" type="submit" value="保存" method="save"/>
<input name="print" type="submit" value="打印" method="print"/>
能给个理由吗
<input name="save" type="submit" value="保存" method="save"/>
<input name="print" type="submit" value="打印" method="print"/>
能给个理由吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
为啥不用button?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询