使用DispatchAction分发

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

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

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

如下:
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;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值