*增删改查之前整合SSH和登录
步骤:
1,编写dao中的增删改查方法,
2,action中调用,
3,Spring中配置,struts中配置,
4,jsp页面,
5,测试
开始代码编写
1,编写dao中的增删改查方法,
增删改查方法
package org.xian.dao;
import java.util.List;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.xian.bean.Users;
public class UserDao {
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public Users findById(int id) {
return hibernateTemplate.get(Users.class, id) ;
}
public void update(Users users ) {
hibernateTemplate.update(users);
}
public List<Users> findAll() {
List list= hibernateTemplate.find("from Users");
return list;
}
public void add(Users users) {
hibernateTemplate.save(users);
}
public void delete(Users users) {
hibernateTemplate.delete(users);
}
public boolean login (String name,String pwd) {
List users= hibernateTemplate.find("from Users where name=? and pwd =?" ,name,pwd);
if (users.size()>0) {
return true;
}
return false;
}
public List checkName (String name) {
List list= hibernateTemplate.find("from Users where name=?" ,name);
return list;
}
}
2,action中调用,
package org.xian.action;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.xian.bean.Users;
import org.xian.dao.UserDao;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class HelloAction extends ActionSupport implements ModelDriven<Users> {
private UserDao dao;
private Users users =new Users();
private List<Users> list;
private List<Users> user;
private Map map;
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public String findById() {
users= dao.findById(users.getUid());
return SUCCESS;
}
public String login() {
if (dao.login(users.getName(), users.getPwd())) {
return SUCCESS;
}
return ERROR;
}
public String getAll() {
list=dao.findAll();
return SUCCESS;
}
public String showAll() {
user=dao.findAll();
map=new HashMap();
map.put("status", "ok");
map.put("show", user);
return SUCCESS;
}
public String update() {
dao.update(users);
return SUCCESS;
}
public String delete() {
dao.delete(users);
return SUCCESS;
}
public String add() {
dao.add(users);
return SUCCESS;
}
public List<Users> getUser() {
return user;
}
public void setUser(List<Users> user) {
this.user = user;
}
public UserDao getDao() {
return dao;
}
public void setDao(UserDao dao) {
this.dao = dao;
}
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public List<Users> getList() {
return list;
}
public void setList(List<Users> list) {
this.list = list;
}
@Override
public Users getModel() {
// TODO Auto-generated method stub
return users;
}
}
3,Spring中配置,struts中配置,
applicationContext.xml配置
<!-- session -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- -action- -->
<bean class="org.xian.action.HelloAction" id="helloAction">
<property name="dao" ref="userDao"></property>
</bean>
<bean class="org.xian.action.ApiAction" id="apiAction">
<property name="dao" ref="userDao"></property>
</bean>
<!-- dao -->
<bean class="org.xian.dao.UserDao" id="userDao">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
</bean>
struts 配置
增删改时需要重定向重新查询一次redirectAction
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<!-- 登录 -->
<action name="login" class="helloAction" method="login">
<result type="redirectAction">getAll</result>
<result name="error">/error.jsp</result>
</action>
<!-- 查询 -->
<action name="getAll" class="helloAction" method="getAll">
<result name="success">/hello.jsp</result>
</action>
<!-- 修改 -->
<action name="update" class="helloAction" method="update">
<result type="redirectAction">getAll</result>
<result name="error">/error.jsp</result>
</action>
<!-- 通过id跳转 -->
<action name="getById" class="helloAction" method="findById">
<result name="success">/edit.jsp</result>
</action>
<!-- 删除 -->
<action name="delete" class="helloAction" method="delete">
<result type="redirectAction">getAll</result>
<result name="error">/error.jsp</result>
</action>
<!-- 增加/注册 -->
<action name="add" class="helloAction" method="add">
<result name="success">/index.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
<package name="json" namespace="/api" extends="json-default">
<!-- json数据 -->
<action name="showAll" class="helloAction" method="showAll">
<result name="success" type="json">
<param name="root">map</param>
</result>
</action>
</package>
</struts>
4,jsp页面,
显示页面
显示页面两种显示方式
使用迭代的方式显示,使用AJAX便利数据
<script type="text/javascript" src="res/jquery.min.js"></script>
<style type="text/css">
#table tr td {
border: solid 1px #666;
}
.red {
background: pink;
}
.blue {
background: orange;
}
div{
color : red;
}
<body onload="all()">
<div align="center">哦吼吼,您好:${users.name}</div>
<hr />
<table align="center" >
<tr>
<td>uid</td>
<td>name</td>
<td>pwd</td>
<td align="center" colspan="2">操作</td>
</tr>
<s:iterator value="list" var="listtt">
<tr>
<td formatter="formatPrice">${listtt.uid}</td>
<td>${listtt.name}</td>
<td>${listtt.pwd}</td>
<td><a href="getById.action?users.uid=${listtt.uid}">修改</a></td>
<td><a href="delete.action?users.uid=${listtt.uid}">删除</a></td>
</tr>
</s:iterator>
</table>
<table id="table" align="center">
<tr>
<td >uid</td>
<td >name</td>
<td>pwd</td>
<td align="center" colspan="2">操作</td>
</tr>
</table>
</body>
<script type="text/javascript">
function all() {
$.ajax({ type : "POST", url : "api/showAll", dataType : "json", success : function(msg) { var json = eval(msg.show); $.each(json, function (index, item) { //循环获取数据 var id = json[index].uid; var name = json[index].name; var pwd = json[index].pwd; item = "<tr><td>"+id+"</td><td>"+name+"</td><td>"+pwd+ "</td><td><a href='getById.action?users.uid="+id +"'>修改</a></td><td><a href='delete.action?users.uid="+ id+"'>删除</a></td></tr>"; $("#table").append(item);
});
$("table tr:odd").addClass("red");
$("table tr:even").addClass("blue");
}
});
}
</script>
增加页面add.jsp
<script type="text/javascript" src="res/jquery.min.js"></script>
<body>
<form action="add">
<h2 align="center">请注册</h2>
<table align="center">
<tr>
<td>用户名:</td>
<td><input type="text" name="name" ></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="pwd"></td>
</tr>
</table>
</form>
</body>
修改页面
导入struts标签库,获取到页面数据,名字密码是可编辑,ID不可编辑
disabled=”disabled”为不可编辑
<%@ taglib uri="/struts-tags" prefix="s"%>
<h3 align="center">您好:${users.name} UID</h3>
<form action="update">
<table align="center">
<tr>
<td>UID:</td>
<td><input type="text" name="name"
value="<s:property value='users.uid' />" disabled="disabled"></td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="name"
value="<s:property value='users.name' />"></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="text" name="pwd"
value="<s:property value='users.name' />"></td>
</tr>
<tr>
<td><input type="submit" value="修改"></td>
</tr>
</table>
</form>
5,测试
登录后显示所有数据,点击注册进行添加数据,点击修改进入修改页面修改数据,点击删除直接删除数据。
增加
显示页面
删除数据
修改数据
修改后