`
jessen163
  • 浏览: 463047 次
  • 性别: Icon_minigender_1
  • 来自: 潘多拉
社区版块
存档分类
最新评论

使用DispatchAction分发

阅读更多
应用中经常有增、删、改、查操作,如果像以前一样使用ListUserAction,AddUserAction等会使action的数量增加,并且同一个模块分散开来不易维护。
使用DispatchAction处理。

要点:
自定义Action如UserAction继承自DispatchAction。

注意不要复写DispatchAction的excute方法,因为DispatchAction的excute方法里面的操作就是获得parameter参数值,并转向到该参数值对应的方法。


如下:
index.jsp
链接到user_list.jsp
 <html:link action="user.do?method=list">User List</html:link>  

user_list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="/share/jsp_head_include.jspf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function addUser(){
	window.self.location = "user/user_input.jsp";
}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="ListUserForm" action="">

<table border="1" >
	<tr>
		<td width="15">Id</td>
		<td width="10">Name</td>
		<td width="10">Age</td>
		<td width="10">Pwd</td>
	</tr>

<logic:present name="userList">
	<c:forEach items="${userList}" var="userModel">
	<tr>
		<td width="15">
			<INPUT type="checkbox" value="${userModel.userId}"/><c:out value="${userModel.userId}"></c:out>
		</td>
		<td width="10"><c:out value="${userModel.userName}"></c:out></td>
		<td width="10"><c:out value="${userModel.age}"></c:out></td>
		<td width="10"><c:out value="${userModel.pwd}"></c:out></td>
	</tr>
	</c:forEach>
</logic:present>
	<tr><td colspan="4">
	<html:button property="btn2" value="Add" onclick="addUser()"></html:button>

	</td></tr>
</table>

</form>
</body>
</html>

user_input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@include file="/share/jsp_head_include.jspf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<html:form action="user.do" method="post">
<input type="hidden" name="method" value="add"/>
	id:<html:text property="userId"></html:text><br>
	name:<html:text property="userName"></html:text><br>
	age:<html:password property="pwd" redisplay="false"/><br>
	<html:submit>submit</html:submit>
	<html:reset/>
</html:form>
</body>
</html>

配置文件
	<action path="/user" type="com.lwf.struts.action.UserAction" name="userForm" parameter="method" >
	      <forward name="success" path="/user/user_list.jsp"></forward>
	      <forward name="addSuccess" path="/user.do?method=list"></forward>
	</action>		

UserAction
package com.lwf.struts.action;

import java.util.List;

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 com.lwf.struts.action.admin.UserForm;
import com.lwf.struts.action.entity.UserModel;
import com.lwf.struts.logic.admin.UserBean;

public class UserAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			List<UserModel> userList = UserBean.getUserList();
			request.setAttribute("userList", userList);
			System.out.println("list");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mapping.findForward("success");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			UserForm userForm = (UserForm)form;
			int id = userForm.getUserId();
			String username = userForm.getUserName();
			String pwd = userForm.getPwd();
			UserBean.addUser(id,username,pwd);
			
		return mapping.findForward("addSuccess");
	}

}


package com.lwf.struts.logic.admin;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.lwf.struts.action.entity.UserModel;
import com.lwf.struts.util.DB;

public class UserBean {

	public static List<UserModel> getUserList() throws Exception{
		List<UserModel> list = new ArrayList<UserModel>();
		String sqlStr = "select * from \"user\"";
		Connection conn = DB.getConnection();
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(sqlStr);
		while(rs.next()){
			UserModel model = new UserModel();
			model.setUserId(rs.getInt("user_id"));
			model.setUserName(rs.getString("user_name"));
			model.setPwd(rs.getString("user_pwd"));
			model.setAge(rs.getInt("age"));
			list.add(model);
		}
		
		return list;
	}
	
	public static void addUser(int id, String username, String pwd) throws Exception{
		Connection conn = DB.getConnection();
		Statement stmt = conn.createStatement();
		String sqlStr = "insert into \"user\"(user_id,user_name,user_pwd) values(" 
			+ id
			+ ", '"
			+ username
			+ "',' "
			+ pwd
			+ "')";
		int n = stmt.executeUpdate(sqlStr);
		System.out.println(n);
		
		
	}
}



package com.lwf.struts.action.entity;

public class UserModel {

	private int userId;
	private String userName;
	private String pwd;
	private int age;
	
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}




package com.lwf.struts.action.admin;

import org.apache.struts.action.ActionForm;

public class UserForm extends ActionForm {

	private int userId;
	private String userName;
	private String pwd;
	
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
}


这里使用UserForm与UserModel实际上可以是一个。

package com.lwf.struts.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DB {
	
	public static Connection getConnection(){
		Connection conn = null;
		String url = "jdbc:postgresql://localhost/FOOD";
		String user = "foodadmin";
		String password = "admin";
		try {
			Class.forName("org.postgresql.Driver");
			
			conn = DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
}




分享到:
评论

相关推荐

    DispatchAction

    它们是Action接口的实现类,主要用于映射HTTP请求到特定的方法,从而实现业务逻辑的分发。 首先,让我们详细了解`DispatchAction`。`DispatchAction`是由Apache Struts框架提供的一个基础类,它扩展了`Action`类。...

    struts DispatchAction

    总之,Struts DispatchAction是Java Web开发中一种有效的请求分发机制,它允许在一个Action类中组织和管理多个业务处理方法,提高了代码的可读性和可维护性。然而,随着技术的发展,如Struts2的出现,开发者可以选择...

    Struts中等同于DispatchAction类方法

    在更现代的Struts版本,如Struts 2,已经提供了更优雅的方式来实现多方法调用,例如使用`@Action`注解和拦截器来实现方法分发,这样既可以保持代码的整洁,又能提高安全性。 总结来说,"Struts中等同于...

    struts+oracle实现DispatchAction类

    在本项目中,我们关注的是"Struts+Oracle实现DispatchAction类",这是一个将Struts框架与Oracle数据库结合使用,以实现业务逻辑处理的例子。 DispatchAction是Struts框架中的一个关键组件,它扮演着控制器的角色,...

    SSH整合继承DispatchAction的简单例子

    这个"SSH整合继承DispatchAction的简单例子"是一个演示如何将这三个框架集成在一起,并利用Struts的DispatchAction来实现多请求分发的功能。 首先,让我们深入理解SSH框架的核心功能。Spring是一个强大的依赖注入...

    ssh+jquery作业分发系统

    由于同事的要求,才给同事的表亲做了这个毕业设计的代码 呵呵。也来献丑了。 里面包含了我构建的完整的框架。...对于formbean及dispatchAction的使用有详细的代码 希望大家批评指正。 jar包请看.classpath文件

    深入解析Java的Struts框架中的控制器DispatchAction

    本文将深入解析Struts中的控制器组件,特别是DispatchAction的使用。 在Struts框架中,控制器主要由ActionServlet和Action类组成。ActionServlet作为前端控制器,负责拦截所有HTTP请求,并根据配置文件(struts-...

    struts1.2实现计算器示例

    这样,当用户发送不同的请求时,`DispatchAction`会根据请求参数调用相应的方法,实现灵活的请求分发。 在计算器示例中,可能包含以下组件: 1. **Action类**:这是`DispatchAction`的子类,包含了多个处理不同...

    struts_02源代码

    2. **15_struts_dispatchaction_usermgr**: DispatchAction 是 Struts 提供的一个特殊Action,它可以将请求分发到多个方法,而不是只调用一个处理方法。Usermgr 可能是用户管理模块,展示如何使用 DispatchAction ...

    struts文档很好的

    `struts_dispatchaction_usermgr`可能是一个示例,展示了如何使用DispatchAction来处理多个子操作。DispatchAction允许在一个Action类中定义多个方法,每个方法对应不同的请求路径,提高了代码复用性。 6. **...

    Struts1.3 备忘笔记

    04 Struts_04DispatchAction : 分发Action,根据设置的参数值提交到不同的方法进行处理业务,演示数学运算 05 Struts_05DynaActionForm : 相对应ActionForm来说,动态ActionForm只需要在配置文件中配置就可以了,...

    spring使用心得 java ssh

    Spring可以通过DispatcherServlet实现请求的分发,Struts的DispatchAction可以被Spring MVC的Controller所替代,这样可以利用Spring的依赖注入和AOP特性,提升应用程序的可维护性和灵活性。 最后,"eclipse.spring...

    Struts高级部分(2)(常用Action、验证框架、动态Form)笔记

    最后,Struts还提供了其他实用工具,如防止表单重复提交的功能、文件上传的支持以及通过DispatchAction实现更复杂的请求分发。这些工具极大地丰富了Struts的功能,使得开发者能够构建出更加健壮和高效的Web应用程序...

    LookupDispatchAction 是使用方法

    public class LookupDispatchAction extends DispatchAction { public ActionForward method1(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ...

    详细动态form案例

    `dispatchAction`可能是一个关于如何在应用中分发动作以触发状态改变的代码片段,这在状态管理中非常常见。而"报错案例"可能包含了一些具体的错误处理示例,展示了如何捕获、解析和展示错误信息给用户。 综上所述,...

    面试知识点总结--struts面试题大全.pdf

    - ActionServlet:核心控制器,负责请求分发和管理配置。 - ActionForm:用于接收和校验用户输入,连接模型和视图。 6. **DispatchAction与Action的区别**: - DispatchAction是可复用的Action,允许在一个类中...

    struts高级日记

    为了解决这个问题,作者推荐使用单元测试的方式来提高测试效率。 #### 单元测试示例 ```java public class BaseActionTest extends MockStrutsTestCase { public BaseActionTest(String arg0) { super(arg0); }...

    《轻量级J2EE企业应用实践-tructs+sping+hibernate整合开发》

    `ForwardAction`和`DispatchAction`是Struts中常见的两种Action,它们分别用于请求转发和分发。`ForwardAction`通常用于将用户请求转发到一个指定的JSP页面或Servlet,而`DispatchAction`则允许根据不同的请求参数...

    STRUTS中文文档.rar

    Action类处理用户请求,Form Bean存储表单数据,DispatchAction用于分发请求到不同的业务逻辑方法,而Forward则定义了请求转发的路径。 "struts的结构.doc"文档应该详细介绍了Struts框架的基本架构,其中包括Action...

Global site tag (gtag.js) - Google Analytics