就是 我做了很多JSP但实际上他们用的都是以张表 添删改查嘛 然后我想不需要每个JSP都要对应一个ACTION 可以让所有JSP指到一个ACTION上 只是这个ACTION里 有为他们4个操作专门对应的方法 可是这个时候问题就出来了 如何让不同的JSP在指向ACTION的时候找出他们需要的在ACTION里的方法呢?如果可以的话写个简单的代码
或者把过程说明一下最好是里面用到那些平时不太接触的方法写以下 谢谢
或者把过程说明一下最好是里面用到那些平时不太接触的方法写以下 谢谢
调试欢乐多
<%@ page language="java" pageEncoding="gbk"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>login.jsp</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<html:form action="/user.do">
用户名:<html:text property="name"></html:text>
密 码:<html:password property="pwd"></html:password>
<html:submit value="增加" property="method" />
<html:submit value="查询" property="method" />
<html:submit value="删除" property="method" />
<html:submit value="修改" property="method" />
</html:form><br>
</body>
</html:html>Struts-config
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
<data-sources />
<form-beans>
<form-bean name="myform" type="com.accp.struts.form.UserActionForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/user" parameter="method" type="com.accp.struts.action.UserAction" name="myform"></action>
</action-mappings>
<message-resources parameter="com.accp.struts.ApplicationResources" />
</struts-config>package com.accp.struts.action;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.DispatchAction;
import org.apache.struts.actions.LookupDispatchAction;public class UserAction extends LookupDispatchAction { public ActionForward insert(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("insert................End");
// TODO Auto-generated method stub
return null;
}
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("delete................End");
// TODO Auto-generated method stub
return null;
}
public ActionForward select(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("select................End");
// TODO Auto-generated method stub
return null;
}
public ActionForward update(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("update................End");
// TODO Auto-generated method stub
return null;
}
@Override
protected String getMethodName(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response,
String parameter) throws Exception {
parameter=request.getParameter(parameter);//取得所传参数
byte[] bytes=parameter.getBytes("ISO8859_1");//中文参数转码
String method=new String(bytes);//得到处理后的参数
if(method.equals("增加"))
{
return "insert";
}
if(method.equals("删除"))
{
return "delete";
}
if(method.equals("查询"))
{
return "select";
}
if(method.equals("修改"))
{
return "update";
}
return "";
} @Override
protected Map getKeyMethodMap() {
// TODO Auto-generated method stub
return null;
}
}
<%@ page language="java" pageEncoding="gbk"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base /> <title>当没有getMethod方法而继承LookupDispatchAction 时(如struts1.1) 则使用界面上的方法</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<html:form action="/user.do">
用户名:<html:text property="name"></html:text>
密 码:<html:password property="pwd"></html:password>
<input type="hidden" name="method" />
<html:submit value="增加" onclick="method.value='insert';"/>
<html:submit value="查询" onclick="method.value='select';" />
<html:submit value="删除" onclick="method.value='delete';" />
<html:submit value="修改" onclick="method.value='update';" />
</html:form><br>
</body>
</html:html>Struts-config
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
<data-sources />
<form-beans>
<form-bean name="myform" type="com.accp.struts.form.UserActionForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/user" parameter="method" type="com.accp.struts.action.UserAction" name="myform"></action>
</action-mappings>
<message-resources parameter="com.accp.struts.ApplicationResources" />
</struts-config>Actionpackage com.accp.struts.action;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.DispatchAction;
import org.apache.struts.actions.LookupDispatchAction;
/**
* 当没有getMethod方法而继承LookupDispatchActio 时(如struts1.1) 则使用界面上的方法
* @author Administrator
*
*/
public class UserAction extends DispatchAction { public ActionForward insert(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Thread.sleep(5000);
System.out.println("insert................End");
// TODO Auto-generated method stub
return null;
}
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("delete................End");
// TODO Auto-generated method stub
return null;
}
public ActionForward select(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("select................End");
// TODO Auto-generated method stub
return null;
}
public ActionForward update(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("update................End");
// TODO Auto-generated method stub
return null;
}
}
login.jsp 和update.jsp
<%@ page language="java" pageEncoding="gbk"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>login.jsp</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<html:form action="/user.do">
用户名:<html:text property="name"></html:text>
密 码:<html:password property="pwd"></html:password>
<input type="hidden" name="method" value="insert"/>
<html:submit value="增加" property="go" />
</html:form><br>
</body>
</html:html>
<%@ page language="java" pageEncoding="gbk"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>update.jsp</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<html:form action="/userupdate.do">
用户名:<html:text property="name"></html:text>
密 码:<html:password property="pwd"></html:password>
<input type="hidden" name="method" value="update"/>
<html:submit value="修改" property="go" />
</html:form><br>
</body>
</html:html><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
<data-sources />
<form-beans>
<form-bean name="myform" type="com.accp.struts.form.UserActionForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/user" parameter="method" type="com.accp.struts.action.UserAction" name="myform"></action>
</action-mappings>
<action-mappings>
<action path="/userupdate" parameter="method" type="com.accp.struts.action.UserAction" name="myform"></action>
</action-mappings>
<message-resources parameter="com.accp.struts.ApplicationResources" />
</struts-confAction Code:package com.ky.action;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.DispatchAction;
import org.apache.struts.actions.LookupDispatchAction;public class UserAction extends DispatchAction { public ActionForward insert(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
return null;
}
public ActionForward update(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
return null;
}
}
然后页面上传了这个参数的值,就对应action里的方法名
一般都是一个模块一个action,不会一个jsp一个action的
主要就是在建立action时,让它继承DispatchAction类,
然后在action类中,写你所用的方法,
在struts-config.xml文件中的action上配置parameter属性,
这个属性用于指定访问时的参数名,
假如你的parameter="par",
那么调用action时用 …….do?par=后面跟action中的某个方法的方法名。
在配置文件中配置:parameter="method"
在JSP中给method赋值,就会调用相应的处理方法
(struts1.2)
添加页面
<form name="form1" action="<%=basePath %>/moreRequest.do?method=add" method="post">
<input type="submit" value="添加">
</form>
删除页面
<form name="form1" action="<%=basePath %>moreRequest.do?method=delete" method="post">
<input type="submit" value="删除">
</form>actionpublic class MoreRequestAction extends Action {
/*
* Generated Methods
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String method = request.getParameter("method");
if(method.equals("add"))
{
return doAdd(mapping,form,request,response);
}
else
if(method.equals("delete"))
{
return doDelete(mapping,form,request,response);
}
else
{
return mapping.findForward("error");
}
} private ActionForward doDelete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String method = request.getParameter("method");
System.out.print(" the method is " + method);
return null;
} private ActionForward doAdd(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String method = request.getParameter("method");
System.out.print(" the method is " + method);
return null;
}
}
struts-config.xml
<action
path="/moreRequest"
type="sourCode.action.MoreRequestAction"/>
其中的<%=basePath %> 可以改成你的工程的名称。
它本身也是一个路径是
<%String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
jsp页面自己生产的。
<action
attribute="DemoForm"
name="DemoForm"
parameter="action" //注意这句话
path="/demo"
scope="request"
validate="false"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="showAdd" contextRelative="true" path="articleLayout_addArticle" />
<forward name="showModify" contextRelative="true" path="articleLayout_modArticle" />
...
</action>
引用的时候url加上<a href="demo.do?action=方法名(如AddArticle)">添加</a>就可以了。然后在DispacthAction编写相应的方法名(如AddArticle)就行了。
public ActionForward addArticle(
final ActionMapping mapping,
final ActionForm form,
final HttpServletRequest request,
final HttpServletResponse response)
throws ServletException {
ArticleForm articleForm = (ArticleForm) form;
articleForm.setCreator(CurrentUser.get().getUserid());
articleForm.setCreatorname(CurrentUser.get().getName()); return mapping.findForward("showAdd");
}
然后Struts 会根据你从页面传过来的这个值来找到对应的方法的。。
传过来的值要和你Action里面的某个方法的名字一样,不然找不到对应的方法就会报错的
这是在CSDN注册以来 回答最有成效的一次了