一个form,多个提交:
只须在jsp的submit标签中添加:
<s:submit value="%{getText('jLogin.submit')}" method="login"/>
<s:submit value="%{getText('jLogin.register')}" method="register"/>
在Action中,将execute改为相应的方法:login(),register()即可.
若要直接的调用,请求参数名的格式为:
action!method.action
只须在jsp的submit标签中添加:
<s:submit value="%{getText('jLogin.submit')}" method="login"/>
<s:submit value="%{getText('jLogin.register')}" method="register"/>
在Action中,将execute改为相应的方法:login(),register()即可.
若要直接的调用,请求参数名的格式为:
action!method.action
实例如下:
【第 1步】实现主页面 (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 >
在 more_submit.jsp中有两个 submit:保存和打印。其中分别通过 method属性指定了要调用的方法: save和 print。因此,在 Action类中必须要有 save和 print方法。
<% @ 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 >
在 more_submit.jsp中有两个 submit:保存和打印。其中分别通过 method属性指定了要调用的方法: save和 print。因此,在 Action类中必须要有 save和 print方法。
【第 2步】实现 Action类( MoreSubmitAction)
package action;
import javax.servlet.http. * ;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor. * ;
import org.apache.struts2.interceptor. * ;
public class MoreSubmitAction extends ActionSupport implements ServletRequestAware
{
private String msg;
private javax.servlet.http.HttpServletRequest request;
// 获得HttpServletRequest对象
public void setServletRequest(HttpServletRequest request)
{
this .request = request;
}
// 处理save submit按钮的动作
public String save() throws Exception
{
request.setAttribute( " result " , " 成功保存[ " + msg + " ] " );
return " save " ;
}
{
private String msg;
private javax.servlet.http.HttpServletRequest request;
// 获得HttpServletRequest对象
public void setServletRequest(HttpServletRequest request)
{
this .request = request;
}
// 处理save submit按钮的动作
public String save() throws Exception
{
request.setAttribute( " result " , " 成功保存[ " + msg + " ] " );
return " save " ;
}
// 处理print submit按钮的动作
public String print() throws Exception
{
request.setAttribute( " result " , " 成功打印[ " + msg + " ] " );
return " print " ;
}
public String getMsg()
{
return msg;
}
public String print() throws Exception
{
request.setAttribute( " result " , " 成功打印[ " + msg + " ] " );
return " print " ;
}
public String getMsg()
{
return msg;
}
public void setMsg(String msg)
{
this .msg = msg;
}
}
{
this .msg = msg;
}
}
上面的代码需要注意如下两点:
save和 print方法必须存在,否则会抛出 java.lang.NoSuchMethodException异常。
Struts2 Action动作的 execute方法无法访问 request对象,因此, Struts2 Action类需要实现一个 Struts2自带的拦截器来获得 request对象,拦截器如下:
org.apache.struts2.interceptor. ServletRequestAware
Struts2 Action动作的 execute方法无法访问 request对象,因此, Struts2 Action类需要实现一个 Struts2自带的拦截器来获得 request对象,拦截器如下:
org.apache.struts2.interceptor. ServletRequestAware
【第 3步】配置 Struts2 Action
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 >
<! 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 >
【第 5步】另一种方式
请求参数名的格式为
action!method.action
action!method.action
大家也可以直接使用如下的 URL来调用 save和 print方法:
调用 save方法: http://localhost:8080/moresubmit/submit!save.action
调用 print方法: http://localhost:8080/moresubmit/submit!print.action
调用 save方法: http://localhost:8080/moresubmit/submit!save.action
调用 print方法: http://localhost:8080/moresubmit/submit!print.action