- 浏览: 1206329 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (718)
- HTML (13)
- JS基础 (23)
- JS应用 (40)
- AJAX (6)
- JSP相关 (12)
- JAVA基础 (52)
- JAVA应用 (74)
- APPLET (11)
- SWING\RCP (2)
- JAVA反射 (6)
- 设计模式 (26)
- 数据库设计 (20)
- Struts (35)
- Struts2 (12)
- Spring (22)
- Hibernate (45)
- Ibatis (18)
- mybatis (3)
- SSH (8)
- UML (5)
- WebService (3)
- XML (16)
- Log4j (7)
- WEB容器 (26)
- 数据结构 (36)
- Linux (34)
- Ruby on Rails (1)
- 其它技术 (27)
- IDE配置 (15)
- 项目实战 (2)
- Oracle (69)
- JAVA报表 (7)
- Android学习 (2)
- 博客链接 (1)
- 网络基础 (1)
- WEB集群 (1)
- .Net开发 (11)
- PB (4)
- 系统构建 (15)
最新评论
-
jnjeC:
牛逼啊哥们,讲得太好了
Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包 -
九尾狐的yi巴:
很好 感谢!
Itext中文处理(更新版) -
luweifeng1983:
有用的,重启一下嘛。
设置eclipse外部修改文件后自动刷新 -
Master-Gao:
设置了也不管用,怎么破呢?
设置eclipse外部修改文件后自动刷新 -
aigo_h:
锋子还有时间写博客,还是很闲哈!
Add directory entries问题
应用中经常有增、删、改、查操作,如果象一前一样使用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 和DynaValidateActionForm 结合使用时的问题
2011-03-08 16:23 904使用DispatchAction使得程序员能够大大减少acti ... -
ActionForm中使用集合属性并自动组装数据
2011-01-18 14:32 909http://blog.csdn.net/m0085_cn/a ... -
Cannot find bean org.apache.struts.taglib.html.BEAN in any scope
2010-11-10 14:13 927开的过程中碰到问题:Cannot find bean org. ... -
Struts 结合 Validate框架验证详解
2010-10-25 14:00 1473转自:http://student.csdn.net/spac ... -
JSTL与struts1标签
2010-08-31 10:17 1517JSTL与struts标签: http://www.360d ... -
如何自动清空struts表单域
2010-06-30 17:44 1242为了某种需要,formBean的SCOPE设置成了ses ... -
form验证与动态验证框架
2010-06-06 17:22 1358form验证返回ActionErrors. 动态验证则实现J ... -
resourcebundleeditor在eclipse里面的设置和使用
2010-05-30 11:30 1709我们一般使用native2ascii工具得到struts资源文 ... -
forward属性与ForwardAction类
2010-04-07 17:08 1145forward属性与ForwardAction类在配置文件按以 ... -
struts容错处理
2010-04-07 15:20 1155struts容错处理: 当客户请求的action不存在的时候 ... -
struts声明式异常二
2010-04-07 15:18 1143上文如果资源文件改为: error.login.usernu ... -
struts声明式异常一
2010-04-06 23:10 1026struts声明式异常: 在配置文件中配置exception ... -
struts编程式异常--html:error与html:message
2010-04-03 11:39 2019struts编程式异常过程: 截获异常信息;创建异常信息; ... -
struts国际化,从资源文件读取异常信息
2010-04-03 01:56 1180示例演示登录时从资源文件读取用户名或密码错误信息 logon ... -
struts国际化,资源文件读取三
2010-04-02 23:42 998本示例演示客户进行语言设置,从而更改整个系统语言. 客户在c ... -
struts国际化,资源文件读取二
2010-04-02 22:18 1607package com.lwf.struts.util; ... -
struts国际化,资源文件读取一
2010-04-02 00:19 1350国际化的发展 看本文之前可先看一下properties文件的 ... -
struts html标签自动保存提交的表单值。
2010-04-01 17:01 2042我们之前JSP提交值后如果还要取得值怎么做? 应该是取得a ... -
ActionMapping相关
2010-04-01 16:02 1049一、Map的设置map.put("path" ... -
ActionForward相关问题
2010-04-01 15:13 1323一、静态的ActionForward不能更改属性: stru ...
相关推荐
一个比较简单的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