<form action="arrayaction.do" method="get">
<input type="button" value="插入"/>
<input type="button" value="删除"/>
<input type="button" value="修改"/>
<input type="button" value="显示"/>
</form>
问题是这样的:页面上有多个按钮,我需要当点击这些按钮时全部进入到同一个action中,但是这个action中有不同的方法.比如insert() delete()等,我点击不同按钮时怎么能够让它找到相应的方法呢?请详细说明,谢谢各位了!!
如果是struts2可以用下面的方法:
将Action类中的每一个处理方法都定义成一个逻辑Action方法。
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="my" extends="struts-default" namespace="/manage">
<action name="userLogin" class="org.qiujy.web.struts2.action.LoginAction" method="login">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="userRegist" class="org.qiujy.web.struts2.action.LoginAction" method="regist">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>如上,把LoginAction中的login和regist方法都配置成逻辑Action。要调用login方法,则相应的把index.jsp中表单元素的action设置为"manage/userLogin.action";要调用regist方法,把regist.jsp中表单元素的action设置为"manage/userRegist.action"。
如果是struts1版本用下列方法
继承action类继承LookUpDispatchAction
第一步:创建一个项目一个jsp页面(MyJsp.jsp)
<html:form action="login" method="post" >
<bean:message key="label.username"/>
<html:text property="userName" />
<bean:message key="label.passWord"/>
<html:password property="password" />
<html:submit property="action">
<bean:message key="button.add"/>
</html:submit>
<html:submit property="action">
<bean:message key="button.update"/>
</html:submit>
<html:errors><!—用于显示验证时出现的错误-->
</html:form>
第二步:applicationResources.properties资源文件代码如下
label.username=usename
label.password=password
button.add=add
button.update=update
error.required={0} is required
error.username.length =Username must longer than 3
error.password.length =Password must longer than 3
第三步:创建一个ActionForm用于封装表单这里我以LoginForm命名代码如下:
package superet.dept.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class LoginForm extends ActionForm {
private String userName;
private String passWord;
public String getUserName() {
return username;
}
public void setUserName(String username) {
this.userName = userName;
}
/**
* Method validate用于验证表单
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if(this.username==null || this.username.length()<1){
ActionMessage error = new ActionMessage("error.required","User Name");
errors.add("user",error);
}
if(this.username.length()<3){
errors.add("userLength",new ActionMessage("error.username.length"));
}
if(this. passWrod ==null || this. passWrod.length()<1){
ActionMessage error = new ActionMessage("error.required","User Name");
errors.add("passWrod ",error);
}
if(this. passWrod.length()<3){
errors.add("userLength",new ActionMessage("error. passWrod.length"));
}
return errors;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
public String getPassWord() {
return password;
}
public void setPassWord(String password) {
this.password = passWord;
}
}
第四步:创建Action这里的Action继承了LookUpDispatchAction我以LoginAction命名.代码如下:
package com.lyx.struts.action;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.LookupDispatchAction;
public class LoginAction extends LookupDispatchAction {
public ActionForward add(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
System.out.print("add");
// TODO Auto-generated method stub
return null;
}
public ActionForward update(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
System.out.println("update");
// TODO Auto-generated method stub
return null;
}
protected Map getKeyMethodMap() {
System.out.print("hello map");
Map map=new HashMap();
map.put("button.add","add");
map.put("button.update"," update");
// TODO 自动生成方法存根
return map;
}
}
注意:这里要把execute方法删掉.要不然不起作用.
getKeyMethodMap()是一个抽象方法,必须得实现.这里用于映射.
第五步:给出Strut-config.xml配置文件的信息.
<?xml version="1.0" encoding="UTF-8"?>
<struts-config>
<data-sources />
<form-beans >
<form-bean name="loginForm" type="com.lyx.struts.form.LoginForm"/>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
input="/MyJsp.jsp"
name="loginForm"
parameter="action"
path="/login"
type="com.lyx.struts.action.LoginAction" />
</action-mappings>
<message-resources parameter="com.lyx.struts.ApplicationResources" />
</struts-config>
注意:这里Action里的Parameter属性的值是表单提交按钮属性的值.
第六步:Ok了.测试一下吧
点Add在控制台上输出Add
点update在控制台上输出update
然后可以使用dispathaction
或者程序判断js不熟的话 随便查下js提交
<action path="" type=""
name=""
scope=""
parameter="action"
input="">
</action>jsp:
<form action="arrayaction.do" method="get">
<input type="button" value="插入" onclick="document.forms[0].action='arrayaction.do?action=insert'"/>
<input type="button" value="删除" onclick="document.forms[0].action='arrayaction.do?action=delete'"//>
<input type="button" value="修改" onclick="document.forms[0].action='arrayaction.do?action=modify'"//>
<input type="button" value="显示" onclick="document.forms[0].action='arrayaction.do?action=display'"//>
<input type="button" value="插入" onClick="document.form[0].action='arrayaction.do?action=insert'"/>
<input type="button" value="删除" onClick="document.form[0].action='arrayaction.do?action=delete'"/>
<input type="button" value="修改" onClick="document.form[0].action='arrayaction.do?action=update'"/>
<input type="button" value="显示" onClick="document.form[0].action='arrayaction.do?action=select'"/>
</form>
能不能说详细些?
path为 /shh/yfadddis.do?method=addDispensary 的addDispensary这样就ok啦。
if (operation!= null && operation.equals("initquery")) {
System.out.println("执行方法initquery")
return initquery(mapping,form,request,response);
}
if (operation != null && operation.equals("query")) {
System.out.println("执行方法query");
return query(mapping,form,request,response);
}
这样就解决了。
struts2就更简单了 通过js提交 直接在action上带上函数就可以了
都提交到同一Action 获取不同的name 调不同方法
onclick="document.forms[0].action='arrayaction.do?action=insert'"form[0]后面的.action是什么意思?我感觉不应该.action呢
怎么在action中获得按钮的name?
怎么在action中获得按钮的name?这个就是在actio里面取form表单的值一样。
struts1里边可以用dispatchAction
具体说一下不就完了么?是用request对象获取么?
给个表达式给我不就完了,我还不知道和获取form表单值一样!!!!有人知道么
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;import com.formbean.ArticleForm;import utils.PageNumber;import bean.Article;
import bean.articleTypeBean;import bean.articleBean;public class articleAction extends DispatchAction {
public ActionForward findAll(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
// List list = abean.findAll();
// request.setAttribute("list", list);
ArticleForm articleForm = (ArticleForm) form;// TODO Auto-generated method stub
articleBean abean = new articleBean();
int typeid=0;//类别编号
int cpage = 1;//当前页
if(request.getParameter("typeid")!=null)
{
typeid = Integer.parseInt(request.getParameter("typeid"));
}
if(request.getParameter("cpage")!=null)
{
cpage = Integer.parseInt(request.getParameter("cpage"));
}
List list = abean.findAll(typeid);
PageNumber<Article> apage = new PageNumber<Article>();
apage.setBigList(list);
apage.setCurrentPage(cpage);
request.setAttribute("apage", apage); // articleBean ab=new articleBean();
// List list=ab.selectAll();
// request.setAttribute("articlelist", list);
request.setAttribute("url", "back_ArticleSelect.jsp");
return mapping.findForward("suc");
}
public ActionForward insertArticle(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
articleTypeBean atb=new articleTypeBean();
List list=atb.selectTypeName();
request.setAttribute("type", list);
request.setAttribute("url", "back_ArticleAdd.jsp");
return mapping.findForward("suc");
}
public ActionForward saveArticle(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
ArticleForm af=(ArticleForm)form;
articleBean ab=new articleBean();
int count= ab.saveArticle(af);
if(count>0){
request.setAttribute("url", "right.jsp");
}else{
request.setAttribute("url", "right.jsp");
}
return mapping.findForward("suc");
}
}
很久以前写的东西了 可能不是很好 个位别笑话啊
用js控制不用的按钮提交到不用的url(insertemp.do/pudateemp.do )public class EmpAction extends MappingDispatchAction { public ActionForward insert(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//.......
return mapping.findForward("success");
} public ActionForward update(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//......
return mapping.findForward("success");
}}struts-config.xml的配置
<action-mappings>
<action path="/insertemp" type="action.EmpAction"
parameter="insert">
</action> <action path="/updateemp" type="action.EmpAction"
parameter="update">
</action>
</action-mappings>
然后让页面分别提交到不同的action。
form.action="/admin/myaction.do?methodp=findNews";
form.submit();后台要配置 <action path="/admin/myaction" name="newsForm" parameter="methodp" scope="request" type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="findNews" path="/admin/news/news_admin.jsp" />
<forward name="editNews" path="/admin/news/edit_news.jsp" />
</action>
action标签中的parameter属性是什么意思?
<action-mappings>
<action validate="false" type="com.accp.action.UserAction" path="/user"
parameter="op">
</action>
继承DispatchAction 不用实现execute定义多个业务控制方法
public class XxxxAction extends DispatchAction {
public ActionForward Add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
return null;
}
hmtl中
在链接至Action时 url中传递一个op参数【为刚才Action配置的parameter】 值为action中的方法名在链接至改Action会自动找到该方法执行如:
<html:link href="/xxxAction?op=Add" >添加</html:link> <html:link href="/xxxAction?op=Select" >查询</html:link>
这样当
url="http://localhost:8080/test/TestAction?xx=Add"
这个xx参数传递的值Add就是我们TestAction中定义的方法
举例
客户端的请求路径为myAction.do?method=fun1,而你的parament的值就是method,他会接受客户端method的值,这里是fun1,然后它回去找你的action里面有没有fun1方法
<form action="arrayaction.do" method="get">
<input type="button" value="插入" name="insert"/>
<input type="button" value="删除" name="delete"/>
<input type="button" value="修改" name="modify"/>
<input type="button" value="显示" name="list"/>
</form>
假如用户点了“插入”按钮,这时:
request.getParameter("insert")=="插入";
request.getParameter("delete")==null;
request.getParameter("modify")==null;
request.getParameter("list")==null;以此类推!也就是说无论多少submit类型的按钮,只有一个不为空。这时在Struts1的Action中以用反射方法来做:
public class MyAction extends Action{//注意,这里并不需要继承DispatchAction public ActionForward execute(ActionMapping mapping, ActionForm from, HttpServletRequest request, HttpServletResponse response) throws Exception {
String func = {"insert","delete","modify","list"};
String methodName = null;
for(String f : func){
if(request.getParameter(f) != null){
methodName = request.getParameter(f);
break;
}
}
try {
//利用反射技术让程序自动找到要处理的方法并执行之,注意methodName参数
Method method = this.clazz.getDeclaredMethod(methodName, ActionMapping.class, ActionForm.class, HttpServletRequest.class, HttpServletResponse.class);
return (ActionForward)method.invoke(this, mapping, from, request, response);
} catch (Exception e) {
request.setAttribute("message", e.getMessage());
return mapping.findForward("message");
}
} public ActionForward insert(ActionMapping mapping, ActionForm from, HttpServletRequest request, HttpServletResponse response) throws Exception {
//todo
} public ActionForward delete(ActionMapping mapping, ActionForm from, HttpServletRequest request, HttpServletResponse response) throws Exception {
//todo
} public ActionForward modify(ActionMapping mapping, ActionForm from, HttpServletRequest request, HttpServletResponse response) throws Exception {
//todo
} public ActionForward list(ActionMapping mapping, ActionForm from, HttpServletRequest request, HttpServletResponse response) throws Exception {
//todo
}}
1,用struts自己的LookupDispatchAction 或dispatchAction两种皆可。。
<form action="arrayaction.do" method="get">
<input type="button" value="插入" onClick="document.form[0].action='arrayaction.do?action=insert'"/>
<input type="button" value="删除" onClick="document.form[0].action='arrayaction.do?action=delete'"/>
<input type="button" value="修改" onClick="document.form[0].action='arrayaction.do?action=update'"/>
<input type="button" value="显示" onClick="document.form[0].action='arrayaction.do?action=select'"/>
</form>8楼的就可以了
当然struts1实现也很容易