dao:package dao;import java.io.Serializable;
import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import entity.User;public class UserDao extends HibernateDaoSupport{ //判断用户是否是系统用户
public User checkUser(User u) {
String sql = "select u from User u where u.name='"
+ u.getName() + "'";
List list = this.search(sql);
if (list.size() > 0) {
User user = (User) list.get(0);
if (user.getPassword().equals(u.getPassword())) {
return user;
}
}
return null;
}
//用户是否已经被注册过了
public User exists(User u)
{
String sql = "select u from User u where u.name='"+ u.getName() + "'";
List list = this.search(sql);
if (list.size() > 0) {
User user=(User)list.get(0);
return user;
}
return null;
}public List search(String hql) {
List ret = super.getHibernateTemplate().find(hql);
return ret;
}
//查询用所有户列表
public List findUsers()
{
String sql=" from User u";
return this.search(sql);
}public User get(Class clzz,Serializable id)
{
return (User)this.getHibernateTemplate().get(clzz, id);
}
//增加用户
public void Add(User user)
{
this.getHibernateTemplate().save(user);
}
//修改用户
public void update(User user)
{
this.getHibernateTemplate().update(user);
}
//删除用户
public void del(Class clzz,Serializable id)
{
this.getHibernateTemplate().delete(this.get(clzz, id));
}
}biz:package biz;import java.io.Serializable;
import java.util.List;import dao.UserDao;
import entity.User;public class UserBiz {private UserDao userDao=null;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}//判断用户是否是系统用户
public User checkUser(User u) {
return this.userDao.checkUser(u);
}
//用户是否已经被注册过了
public User exists(User u)
{
return userDao.exists(u);
}
//增加用户
public void Add(User user)
{
userDao.Add(user);
}
public List findUsers()
{
return userDao.findUsers();
}
public void del(Class clzz,Serializable id)
{
userDao.del(clzz, id);
}
//修改用户
public void update(User user)
{
userDao.update(user);
}public User get(Class clzz,Serializable id)
{
return userDao.get(clzz, id);
}
}action::package web.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 entity.User;
import web.form.UserForm;import biz.UserBiz;public class UserAction extends DispatchAction {private UserBiz userBiz=null;public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}/*
* 登录
*/
public ActionForward doLogin(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
UserForm form2=(UserForm) form;
User user=new User();
user.setName(form2.getItem().getName());
user.setPassword(form2.getItem().getPassword());
User u= userBiz.checkUser(user);
if(u!=null){
return mapping.findForward("toList");
}else{
return mapping.findForward("failure");
}
}//查询用户列表
public ActionForward listAll(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
List list=this.userBiz.findUsers();
request.setAttribute("list", list);
return mapping.findForward("list");
}//增加用户
public ActionForward register(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
UserForm uf=(UserForm)form;
User u=uf.getItem();
User user=userBiz.exists(u);
if(user==null)
{
this.userBiz.Add(u);
System.out.println("Add OK");
return mapping.findForward("toList");
}else
{
System.out.println("Exitst");
return mapping.findForward("re");
}
} //删除用户
public ActionForward DelUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
int id=Integer.parseInt(request.getParameter("id"));
userBiz.del(User.class, id);
return mapping.findForward("toList");
}//显示用户信息
public ActionForward DoUpdate(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
UserForm myForm = (UserForm)form;
int id=Integer.parseInt(request.getParameter("id"));
User user=this.userBiz.get(User.class, id);
myForm.setItem(user);
return mapping.findForward("update");
}//修改用户信息
public ActionForward ToUpdate(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
UserForm user=(UserForm)form;
this.userBiz.update(user.getItem());
return mapping.findForward("toList");
}
}form:package web.form;import org.apache.struts.action.ActionForm;import entity.User;public class UserForm extends ActionForm{private User item=new User();public User getItem() {
return item;
}public void setItem(User item) {
this.item = item;
}
}struts-config.xm:<?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="userForm" type="web.form.UserForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/user" name="userForm" parameter="o" type="org.springframework.web.struts.DelegatingActionProxy" scope="request">
<forward name="login" path="/index.jsp"></forward>
<forward name="toList" path="/user.do?o=listAll"></forward>
<forward name="list" path="/List.jsp"></forward>
<forward name="failure" path="/failure.jsp"></forward>
<forward name="re" path="/register.jsp"></forward>
<forward name="update" path="/update.jsp"></forward>
</action>
</action-mappings><message-resources parameter="com.test.ApplicationResources" /> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="classpath:applicationContext.xml"/>
</plug-in>
</struts-config>applicationContext.xml:<?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="userForm" type="web.form.UserForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/user" name="userForm" parameter="o" type="org.springframework.web.struts.DelegatingActionProxy" scope="request">
<forward name="login" path="/index.jsp"></forward>
<forward name="toList" path="/user.do?o=listAll"></forward>
<forward name="list" path="/List.jsp"></forward>
<forward name="failure" path="/failure.jsp"></forward>
<forward name="re" path="/register.jsp"></forward>
<forward name="update" path="/update.jsp"></forward>
</action>
</action-mappings><message-resources parameter="com.test.ApplicationResources" /> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="classpath:applicationContext.xml"/>
</plug-in>
</struts-config>List:
<%@ page language="java" pageEncoding="GB18030"%><%@ 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" %>
<html>
<head>
<title>List.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>
<form action="" method="post"> <table width="663" height="266" border="1" align="center" cellspacing="0">
<tr align="center">
<td colspan="5"><h1><span class="STYLE3">用户信息</span></h1><br></td>
</tr>
<tr align="center">
<td width="128">用户Id</td>
<td width="108">用户名</td>
<td width="81">密码</td>
<td width="81"> </td>
<td width="81"> </td>
</tr>
<logic:iterate id="tt" name="list">
<tr align="center">
<td width="128">${tt.id}</td>
<td width="108">${tt.name }</td>
<td width="81">${tt.password}</td>
<td width="81"><a href="user.do?o=DelUser&id=${tt.id}">删除</a></td>
<td width="81"><a href="user.do?o=DoUpdate&id=${tt.id}">修改</a></td>
</tr>
</logic:iterate>
</table> </form>
</body>
</html>
import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import entity.User;public class UserDao extends HibernateDaoSupport{ //判断用户是否是系统用户
public User checkUser(User u) {
String sql = "select u from User u where u.name='"
+ u.getName() + "'";
List list = this.search(sql);
if (list.size() > 0) {
User user = (User) list.get(0);
if (user.getPassword().equals(u.getPassword())) {
return user;
}
}
return null;
}
//用户是否已经被注册过了
public User exists(User u)
{
String sql = "select u from User u where u.name='"+ u.getName() + "'";
List list = this.search(sql);
if (list.size() > 0) {
User user=(User)list.get(0);
return user;
}
return null;
}public List search(String hql) {
List ret = super.getHibernateTemplate().find(hql);
return ret;
}
//查询用所有户列表
public List findUsers()
{
String sql=" from User u";
return this.search(sql);
}public User get(Class clzz,Serializable id)
{
return (User)this.getHibernateTemplate().get(clzz, id);
}
//增加用户
public void Add(User user)
{
this.getHibernateTemplate().save(user);
}
//修改用户
public void update(User user)
{
this.getHibernateTemplate().update(user);
}
//删除用户
public void del(Class clzz,Serializable id)
{
this.getHibernateTemplate().delete(this.get(clzz, id));
}
}biz:package biz;import java.io.Serializable;
import java.util.List;import dao.UserDao;
import entity.User;public class UserBiz {private UserDao userDao=null;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}//判断用户是否是系统用户
public User checkUser(User u) {
return this.userDao.checkUser(u);
}
//用户是否已经被注册过了
public User exists(User u)
{
return userDao.exists(u);
}
//增加用户
public void Add(User user)
{
userDao.Add(user);
}
public List findUsers()
{
return userDao.findUsers();
}
public void del(Class clzz,Serializable id)
{
userDao.del(clzz, id);
}
//修改用户
public void update(User user)
{
userDao.update(user);
}public User get(Class clzz,Serializable id)
{
return userDao.get(clzz, id);
}
}action::package web.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 entity.User;
import web.form.UserForm;import biz.UserBiz;public class UserAction extends DispatchAction {private UserBiz userBiz=null;public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}/*
* 登录
*/
public ActionForward doLogin(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
UserForm form2=(UserForm) form;
User user=new User();
user.setName(form2.getItem().getName());
user.setPassword(form2.getItem().getPassword());
User u= userBiz.checkUser(user);
if(u!=null){
return mapping.findForward("toList");
}else{
return mapping.findForward("failure");
}
}//查询用户列表
public ActionForward listAll(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
List list=this.userBiz.findUsers();
request.setAttribute("list", list);
return mapping.findForward("list");
}//增加用户
public ActionForward register(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
UserForm uf=(UserForm)form;
User u=uf.getItem();
User user=userBiz.exists(u);
if(user==null)
{
this.userBiz.Add(u);
System.out.println("Add OK");
return mapping.findForward("toList");
}else
{
System.out.println("Exitst");
return mapping.findForward("re");
}
} //删除用户
public ActionForward DelUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
int id=Integer.parseInt(request.getParameter("id"));
userBiz.del(User.class, id);
return mapping.findForward("toList");
}//显示用户信息
public ActionForward DoUpdate(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
UserForm myForm = (UserForm)form;
int id=Integer.parseInt(request.getParameter("id"));
User user=this.userBiz.get(User.class, id);
myForm.setItem(user);
return mapping.findForward("update");
}//修改用户信息
public ActionForward ToUpdate(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
UserForm user=(UserForm)form;
this.userBiz.update(user.getItem());
return mapping.findForward("toList");
}
}form:package web.form;import org.apache.struts.action.ActionForm;import entity.User;public class UserForm extends ActionForm{private User item=new User();public User getItem() {
return item;
}public void setItem(User item) {
this.item = item;
}
}struts-config.xm:<?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="userForm" type="web.form.UserForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/user" name="userForm" parameter="o" type="org.springframework.web.struts.DelegatingActionProxy" scope="request">
<forward name="login" path="/index.jsp"></forward>
<forward name="toList" path="/user.do?o=listAll"></forward>
<forward name="list" path="/List.jsp"></forward>
<forward name="failure" path="/failure.jsp"></forward>
<forward name="re" path="/register.jsp"></forward>
<forward name="update" path="/update.jsp"></forward>
</action>
</action-mappings><message-resources parameter="com.test.ApplicationResources" /> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="classpath:applicationContext.xml"/>
</plug-in>
</struts-config>applicationContext.xml:<?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="userForm" type="web.form.UserForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/user" name="userForm" parameter="o" type="org.springframework.web.struts.DelegatingActionProxy" scope="request">
<forward name="login" path="/index.jsp"></forward>
<forward name="toList" path="/user.do?o=listAll"></forward>
<forward name="list" path="/List.jsp"></forward>
<forward name="failure" path="/failure.jsp"></forward>
<forward name="re" path="/register.jsp"></forward>
<forward name="update" path="/update.jsp"></forward>
</action>
</action-mappings><message-resources parameter="com.test.ApplicationResources" /> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="classpath:applicationContext.xml"/>
</plug-in>
</struts-config>List:
<%@ page language="java" pageEncoding="GB18030"%><%@ 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" %>
<html>
<head>
<title>List.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>
<form action="" method="post"> <table width="663" height="266" border="1" align="center" cellspacing="0">
<tr align="center">
<td colspan="5"><h1><span class="STYLE3">用户信息</span></h1><br></td>
</tr>
<tr align="center">
<td width="128">用户Id</td>
<td width="108">用户名</td>
<td width="81">密码</td>
<td width="81"> </td>
<td width="81"> </td>
</tr>
<logic:iterate id="tt" name="list">
<tr align="center">
<td width="128">${tt.id}</td>
<td width="108">${tt.name }</td>
<td width="81">${tt.password}</td>
<td width="81"><a href="user.do?o=DelUser&id=${tt.id}">删除</a></td>
<td width="81"><a href="user.do?o=DoUpdate&id=${tt.id}">修改</a></td>
</tr>
</logic:iterate>
</table> </form>
</body>
</html>
解决方案 »
- 【servlet】在做第一个servlet程序的时候就出现问题,新手在线求解答
- jstl表达式中foreach的问题
- 用一个按钮控制另一个按钮在火狐中如何实现呢?
- jspsmartupload中的getWriter()问题,请高手指教
- 为什么jsp有时候会显示空白页?
- 怎么在jsp文件里嵌入Servlet生成的图像?
- 提交表单时,如何判断列表值是否为空?
- hibernate报错,是varbinary数据类型的映射错误么?急
- 通过jquery调用Axis2 WebService,在IE上返回正确结果,但在Firefox和Chrome上却返回错误,附简单代码,求解!
- 编译JSP页面出现Unable to compile class for JSP错误
- GWT布局
- hibernate无法创建session
update.jsp:<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ 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>
<head>
<base href="<%=basePath%>">
<title>My JSP 'update.jsp' starting page</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>
<form method="post" action="user.do?o=ToUpdate">
<table width="663" height="266" border="1" align="center" cellspacing="0">
<tr align="center">
<td colspan="5"><h1><span class="STYLE3">用户修改信息</span></h1><br></td>
</tr>
<tr align="center">
<td width="108">用户名</td>
<td width="81">密码</td>
</tr>
<tr align="center">
<td width="128"> <input type="text" name="name" value="${item.name}"> </td>
<td width="108"><input type="text" name="password" value="${item.password}"></td> </tr>
<tr align="center">
<td><input type="reset" name="button" id="button" value="重置"></td>
<td><input type="submit" name="button2" id="button2" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>现在的问题是:点击修改,跳转到update那边,本来应该显示某一用户的资料出来的,可是显示不了,而且也修改不了。我真的很郁闷,id也传递过去了。这是stucts1.2 spring2.0 h 3.1; 希望有人给我说说,谢谢。
UserForm myForm = (UserForm)form;
的myForm 不能获取值,
因为你点击“修改”--它不是提交到Form,说哟Form过去不了值
你可以在Doupdate方法里边这样写:
根据Id查询出用户来以后,把用户对象存到Session里边,如下:
Request.getSession.setAttribute("user",user);
然后再ToUpdate里边更新user时,可直接再session里边获取:
Users user =request.getSession.getAttribute("user");完整代码如下:
//显示用户信息
public ActionForward DoUpdate(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
int id=Integer.parseInt(request.getParameter("id"));
User user=this.userBiz.get(User.class, id);
Request.getSession.setAttribute("user",user);
return mapping.findForward("update");
}//修改用户信息
public ActionForward ToUpdate(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
Users user =request.getSession.getAttribute("user");
this.userBiz.update(user)
return mapping.findForward("toList");
}
}