赞
踩
搭建SSH框架请参考文章(http://blog.csdn.net/qq_39189632/article/details/78401657)
1.创建User.java
package com.user.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* 用户表
* @author LICHENGLONG
*
*/
@Entity
@Table(name="tb_user")
public class User {
private Integer id;
private String username;
private String password;
private Integer userid;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="username")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name="password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name="userid")
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + ", userid=" + userid + "]";
}
}
2、创建UserDao.java
package com.user.dao;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.user.model.User;
@Repository(value="userDao")
public class UserDao extends HibernateDaoSupport {
boolean bool = false;
@Resource(name="sessionFactory")
public void setSuperSessionFactory(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}
/**
* 新增
* @param obj
* @return
*/
public boolean save(Object obj){
try {
super.getHibernateTemplate().save(obj);
bool = true;
} catch (Exception e) {
bool = false;
}
return bool;
}
/**
* 修改
* @param obj
* @return
*/
public boolean update(Object obj){
try {
super.getHibernateTemplate().update(obj);
bool = true;
} catch (Exception e) {
bool = false;
}
return bool;
}
/**
* 删除
* @param obj
* @return
*/
public boolean delete(Object obj){
try {
super.getHibernateTemplate().delete(obj);
bool = true;
} catch (Exception e) {
bool = false;
}
return bool;
}
/**
* 查询
* @param hql
* @return
*/
public List<User> find(String hql){
return super.getHibernateTemplate().find(hql);
}
}
3.创建接口IUserService.java
package com.user.service;
import java.util.List;
import com.user.model.User;
public interface IUserService {
public abstract boolean save(User user);
public abstract List<User> listUser();
public abstract List<User> listUserId(Integer id);
public abstract List<User> listUserLogin(String username,String password);
public abstract boolean update(User user);
public abstract boolean delete(User user);
}
4、创建UserService.java
package com.user.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.user.dao.UserDao;
import com.user.model.User;
import com.user.service.IUserService;
@Service
public class UserService implements IUserService {
@Resource(name="userDao")
private UserDao userDao;
public boolean save(User user) {
boolean bool = userDao.save(user);
return bool;
}
public List<User> listUser() {
List<User> listUser = userDao.find("from User");
return listUser;
}
public List<User> listUserId(Integer id) {
List<User> listUser = userDao.find("from User where id="+id);
return listUser;
}
public List<User> listUserLogin(String username, String password) {
List<User> listUser = userDao.find("from User where username="+"'"+username+"'"+" and password="+"'"+password+"'");
return listUser;
}
public boolean update(User user) {
boolean bool = userDao.update(user);
return bool;
}
public boolean delete(User user) {
boolean bool = userDao.delete(user);
return bool;
}
}
5、创建 UserAction.java
package com.user.action;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.opensymphony.xwork2.ActionSupport;
import com.user.model.User;
import com.user.service.IUserService;
@Scope("prototype")
@Component(value="user.userAction")
public class UserAction extends ActionSupport {
@Autowired
private IUserService userService;
private Integer id;
private String username;
private String password;
private Integer userid;
private List<User> listUser;
public String add(){
if(!username.equals("") && !password.equals("") && userid > 0){
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setUserid(userid);
boolean bool = userService.save(user);
if (bool) {
return "login";
}
}
return "add";
}
public String userlogin(){
if(!username.equals("") && !password.equals("")){
System.out.println("username:"+username+",password:"+password);
List<User> listUser = userService.listUserLogin(username, password);
System.out.println(listUser);
if(listUser.size() > 0){
return findData();
}
}
return "login";
}
public String findData(){
listUser = userService.listUser();
return "main";
}
public String findDatauserId(){
if(id > 0){
listUser = userService.listUserId(id);
return "update";
}
return findData();
}
public String update(){
if(id > 0 && !username.equals("") && !password.equals("") && userid > 0){
User user = new User();
user.setId(id);
user.setUsername(username);
user.setPassword(password);
user.setUserid(userid);
boolean bool = userService.update(user);
if (bool) {
return findData();
}
}
return "update";
}
public String delete(){
if(id > 0){
User user = new User();
user.setId(id);
boolean bool = userService.delete(user);
if (bool) {
return findData();
}
}
return findData();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public List<User> getListUser() {
return listUser;
}
public void setListUser(List<User> listUser) {
this.listUser = listUser;
}
}
6、创建 struts-user.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="user" extends="struts-default" namespace="/user">
<action name="*User" class="user.userAction" method="{1}">
<result name="login">/login.jsp</result>
<result name="add">/add.jsp</result>
<result name="main">/main.jsp</result>
<result name="update">/update.jsp</result>
</action>
</package>
</struts>
7、创建 struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<include file="struts-user.xml"></include>
</struts>
1、创建login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.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 action="<%=path %>/user/userloginUser.action" method="post">
<table>
<tr>
<td>用户:</td>
<td><input type="text" name="username" ></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" ></td>
</tr>
<tr>
<td><a href="<%=path %>/add.jsp">注册</a></td>
<td><input type="submit" value="登录" ></td>
</tr>
</table>
</form>
</body>
</html>
2、创建 main.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'main.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>
<table>
<tr>
<th>id</th>
<th>username</th>
<th>password</th>
<th>userid</th>
<th colspan="3">编辑</th>
</tr>
<c:forEach items="${listUser }" var="data" >
<tr>
<td>${data.id }</td>
<td>${data.username }</td>
<td>${data.password }</td>
<td>${data.userid }</td>
<td><a href="<%=path %>/add.jsp">新增</a></td>
<td><a href="<%=path %>/user/findDatauserIdUser.action?id=${data.id }">修改</a></td>
<td><a href="<%=path %>/user/deleteUser.action?id=${data.id }">删除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
3、创建add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'add.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 action="<%=path %>/user/addUser.action" method="post">
<table>
<tr>
<td>用户名称:</td>
<td><input type="text" name="username" ></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" ></td>
</tr>
<tr>
<td>用户ID:</td>
<td><input type="text" name="userid" ></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="注册" ></td>
</tr>
</table>
</form>
</body>
</html>
4、创建 update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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 action="<%=path %>/user/updateUser.action" method="post">
<c:forEach items="${listUser}" var="data">
<input type="hidden" name="id" value="${data.id }" >
<table>
<tr>
<td>用户名称:</td>
<td><input type="text" name="username" value="${data.username }" ></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" value="${data.password }" ></td>
</tr>
<tr>
<td>用户ID:</td>
<td><input type="text" name="userid" value="${data.userid }" ></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="修改" ></td>
</tr>
</table>
</c:forEach>
</form>
</body>
</html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。