应用中经常有增、删、改、查操作,如果像以前一样使用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;
}
}
分享到:
相关推荐
一个比较简单的struts的dispatchAction 类的入门案例,适合初学者理解dispatchAction
DispatchAction与MappingDispatchAction
struts中的DispatchAction应用实例
解决Struts1.x中使用Validator框架对DispatchAction不同方法使用不同的字段验证 你可以先参看我的blog文章:http://blog.csdn.net/qjyong/archive/2008/11/16/3311688.aspx 再决定是否下载。
简单的DispatchAction实例。。。。。。。。
DispatchAction的作用简单地说就是把原来我们写在多个acton里的操作放在同一个 action里处理。
Struts(DispatchAction) + 存储过程 => 实现分页效果
在页面中用js传递执行方法参数,传递到后台执行, 在你用的Action类中的方法名前加“run“及可被调用。
最近自学java中的框架-struts写了一些小例子,这都是很经典的程序,如果大家瞧得起要下载去看看,顺便给俺找找不足的地方。我的qq 821865130 email qingtian_hechen@163.com 希望大家能多多给我帮助...在此谢谢各位!!
03Struts2常用标签&DispatchAction.rar03Struts2常用标签&DispatchAction.rar03Struts2常用标签&DispatchAction.rar03Struts2常用标签&DispatchAction.rar
Struts 之 DispatchAction add
Struts1中怎样实现DispatchAction配置多个input属性\Struts1中怎样实现DispatchAction配置多个input属性
由于同事的要求,才给同事的表亲做了这个毕业设计的代码 呵呵。也来献丑了。 里面包含了我构建的完整的框架。...对于formbean及dispatchAction的使用有详细的代码 希望大家批评指正。 jar包请看.classpath文件
利用struts标签,dispatchAction,实现增删改查 有利于初学者掌握struts!
这是个SSH整合的简单例子,Action是继承自DispatchAction,多个JSP页面或是form可以跳转到这一个Action中调用同一个或是不同滴方法,有兴趣滴可以看一下,绝对无误!
自习struts时做时,大家可以互相交流一下
主要介绍了深入解析Java的Struts框架中的控制器DispatchAction,Struts是Java的SSH三大web开发框架之一,需要的朋友可以参考下
青鸟搜藏包括功能有添加书签,书签列表,使用DispatchAction、Struts报错机制、Struts标签!!!
04 Struts_04DispatchAction : 分发Action,根据设置的参数值提交到不同的方法进行处理业务,演示数学运算 05 Struts_05DynaActionForm : 相对应ActionForm来说,动态ActionForm只需要在配置文件中配置就可以了,...
Struts 之 DispatchAction