ssh文件目录结构如下:
web.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>ssh3</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<display-name>StrutsPrepareAndExecuteFilter</display-name>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring-*.xml</param-value>
</context-param>
</web-app>
spring-struts.xml文件内容
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
default-lazy-init="true"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="cn.tedu.action"/>
<context:component-scan base-package="cn.tedu.web"/>
</beans>
spring-service.xml文件内容如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
default-lazy-init="true"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="cn.tedu.service"/>
</beans>
spring-hiberate.xml文件内容如下
<!-- /** 配置文件描述: spring-mvc配置 */ -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
default-lazy-init="true"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- spring-hibernate.xml -->
<!-- 配置数据源,连接到数据库,替代了hibernate的主配置文件 hibernate.xfg.xml -->
<!-- 读取properties -->
<util:properties id="conf" location="classpath:conf/conf.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="#{conf.driver}"/>
<property name="url" value="#{conf.url}"/>
<property name="username" value="#{conf.username}"/>
<property name="password" value="#{conf.password}"/>
<property name="initialSize" value="#{conf.initialSize}"/>
<property name="maxActive" value="#{conf.maxActive}"/>
<property name="minIdle" value="#{conf.minIdle}"/>
<property name="maxWait" value="#{conf.maxWait}"/>
<property name="timeBetweenLogStatsMillis" value="#{conf.timeBetweenLogStatsMillis}"/>
</bean>
<!-- 利用spring-orm 提供的工厂Bean创建SessionFactory 对象 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 配置数据源,使hibernate能够连接到数据库 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置hibernate工作参数,包括SQL方言 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 配置“子映射”文件的存储位置 -->
<property name="mappingLocations">
<value>classpath:hbm/*.xml</value>
</property>
</bean>
<!-- Spring提供了hibernateTemplate封装了Session,提供了与Session
几乎一样的功能,但是使用此session更简单 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置hibernate 事务管理器 -->
<bean id="txMgr" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 开启基于注解的声明式事务管理 -->
<tx:annotation-driven transaction-manager="txMgr"/>
<context:component-scan base-package="cn.tedu.dao"/>
</beans>
struts.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 从 struts-2.5.dtd 文件中复制 DOCTYPE -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.action.extension"
value="do"/>
<package name="user" namespace="/user"
extends="json-default">
<!-- 登记拦截组件 -->
<interceptors>
<interceptor name="exp"
class="exceptionInterceptor">
</interceptor>
<interceptor-stack name="allStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="exp"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="allStack"></default-interceptor-ref>
<global-results>
<result type="json" name="json">
<param name="root">result</param>
</result>
</global-results>
<action name="list" class="userAction"
method="list"></action>
<action name="save" class="userAction"
method="save"></action>
<action name="load" class="userAction"
method="load">
</action>
<action name="update" class="userAction"
method="update"></action>
</package>
<package namespace="/u" name="u" extends="json-default">
<action name="list" class="usrAction" method="list">
<result name="success">
/WEB-INF/jsp/list.jsp
</result>
</action>
</package>
</struts>
Uset.hbm.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 定义了 类和表的对应关系 -->
<class name="cn.tedu.entity.User"
table="t_user">
<!-- 定义属性和列(字段)的对应关系 -->
<id name="id" column="u_id">
<generator class="identity"></generator>
</id>
<property name="name" column="u_name"/>
<property name="password" column="u_password"/>
<property name="age" column="u_age"/>
<property name="salary" column="u_salary"/>
<property name="hiredate" column="u_hiredate"/>
</class>
</hibernate-mapping>
entity包下的User
package cn.tedu.entity;
import java.io.Serializable;
import java.sql.Date;
public class User implements Serializable{
private static final long serialVersionUID = -4217905182474304107L;
private Integer id;
private String name;
private String password;
private Integer age;
private Double salary;
private Date hiredate;
public User() {
}
public User(Integer id, String name, String password, Integer age, Double salary, Date hiredate) {
super();
this.id = id;
this.name = name;
this.password = password;
this.age = age;
this.salary = salary;
this.hiredate = hiredate;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + ", age=" + age + ", salary=" + salary
+ ", hiredate=" + hiredate + "]";
}
}
UserDao
package cn.tedu.dao;
import java.util.List;
import java.util.Map;
import cn.tedu.entity.User;
public interface UserDao {
int addUser(User user);
int updateUser(User user);
int deleteUser(Integer id);
User findById(Integer id);
List<User> findAll();
User findByName(String name);
List<Map<String,Object>> findAllUser();
int findMaxId();
}
UserDaoImpl
package cn.tedu.dao.impl;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import cn.tedu.dao.UserDao;
import cn.tedu.entity.User;
@Repository("userDao")
@Transactional
public class UserDaoImpl implements UserDao{
@Resource
private HibernateTemplate hibernateTemplate;
public int addUser(User user) {
Object id=hibernateTemplate.save(user);
return id==null ? 0 : 1;
}
public int updateUser(User user) {
hibernateTemplate.update(user);
return 1;
}
public int deleteUser(Integer id) {
User user=hibernateTemplate.get(User.class,id);
hibernateTemplate.delete(user);
return 1;
}
public User findById(Integer id){
User user=hibernateTemplate.get(User.class, id);
return user;
}
public List<User> findAll() {
String hql="from User";
List<User> list=(List<User>) hibernateTemplate.find(hql);
return list;
}
public User findByName(String name) {
String hql="from User where name=?";
List<User> list=(List<User>) hibernateTemplate.find(hql,name);
if(list.isEmpty()) {
return null;
}
return list.get(0);
}
public List<Map<String, Object>> findAllUser() {
String hql="select new map(id as id,name as name,age as age) from User";
List<Map<String,Object>> list=(List<Map<String, Object>>) hibernateTemplate.find(hql);
return list;
}
public int findMaxId() {
//sql:select max(u_id) from t_user
String hql="select max(id) from User";
List<Number> list=(List<Number>) hibernateTemplate.find(hql);
return list.get(0).intValue();
}
}
UserService
package cn.tedu.service;
import java.util.List;
import java.util.Map;
import cn.tedu.entity.User;
public interface UserService {
List<Map<String,Object>> userList();
boolean saveUser(String name,String password,Integer age,Double salary);
User loadUser(Integer id);
void updateUser(Integer id, String name, String password, Integer age, Double salary);
}
UserServiceImpl
package cn.tedu.service;
import java.sql.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.management.RuntimeErrorException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.tedu.dao.UserDao;
import cn.tedu.entity.User;
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService{
@Resource
private UserDao userDao;
public List<Map<String, Object>> userList() {
return userDao.findAllUser();
}
public boolean saveUser(String name, String password, Integer age, Double salary) {
long now=System.currentTimeMillis();
//int max=userDao.findMaxId();
//int id=max+1;
Integer id= null;
User user=new User(id,name,password,age,salary,new Date(now));
int n=userDao.addUser(user);
return n==1;
}
public User loadUser(Integer id) {
if(id==null) {
throw new RuntimeException("id 不能为空");
}
return userDao.findById(id);
}
public void updateUser(Integer id, String name, String password, Integer age, Double salary) {
User user=userDao.findById(id);
user.setAge(age);
user.setName(name);
user.setSalary(salary);
user.setPassword(password);
long now=System.currentTimeMillis();
user.setHiredate(new Date(now));
userDao.updateUser(user);
}
}
UserActon
package cn.tedu.action;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.tedu.entity.User;
import cn.tedu.service.UserService;
import cn.tedu.util.JsonResult;
@Controller
@Scope("prototype")
public class UserAction {
@Resource
private UserService userService;
private JsonResult result;
public JsonResult getResult() {
return result;
}
public void setResult(JsonResult result) {
this.result = result;
}
public String list(){
List<Map<String,Object>> list=userService.userList();
System.out.println("list:");
for(Map<String,Object> map:list) {
System.out.println(map);
}
result=new JsonResult(list);
System.out.println(result);
return "json";
}
private String name;
private String password;
private Integer age;
private Double salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public String save(){
boolean ok=userService.saveUser(name,password,age,salary);
result=new JsonResult(ok);
return "json";
}
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String load() {
User user=userService.loadUser(id);
result=new JsonResult(user);
return "json";
}
public String update() {
userService.updateUser(id,name,password,age,salary);
result=new JsonResult("ok");
return "json";
}
}
UsrAction
package cn.tedu.action;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.struts2.interceptor.RequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.tedu.service.UserService;
/*@scope默认是单例模式(singleton)
如果需要设置的话@scope("prototype")
1.singleton单例模式,全局有且仅有一个实例
2.prototype原型模式,每次获取Bean的时候会有一个新的实例*/
@Controller
@Scope("prototype")
public class UsrAction implements RequestAware{
@Resource
private UserService userService;
private List<Map<String,Object>> users;
private Map<String,Object> request;
private String myMessage;
public String getMyMessage() {
return myMessage;
}
public void setMyMessage(String myMessage) {
this.myMessage = myMessage;
}
public void setRequest(Map<String, Object> request) {
this.request=request;
}
public List<Map<String, Object>> getUsers() {
return users;
}
public void setUsers(List<Map<String, Object>> users) {
this.users = users;
}
public String list() {
List<Map<String,Object>> list=userService.userList();
users=list;
request.put("message", "Hello World!");
myMessage="Hello Value Stack!";
return "success";
}
}
页面
<%@ page
contentType="text/html; charset=utf-8"
pageEncoding="utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body style="font-size:30px;">
<h1>用户管理</h1>
<table>
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>
</c:forEach>
</tbody>
</table>
<h2>显示valueStack信息</h2>
<h2>显示上下文信息</h2>
<p><s:property value="#request.message"></s:property></p>
<h2>显示内容信息</h2>
<p><s:property value="myMessage"></s:property></p>
<s:debug></s:debug>
<h1>用户管理 OGNL表达式</h1>
<table>
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<s:iterator value="users">
<tr>
<td><s:property value="id"/></td>
<td><s:property value="name"/></td>
<td><s:property value="age"/></td>
</tr>
</s:iterator>
</tbody>
</table>
</body>
</html>