s2sh整合
最近的struts2-hibernate -spring整合开发
1.简单讲解spring和struts2整合
首先导入struts的jar,hibernate的jar,spring的jar,数据库的驱动包XX.jar放入lib包中。
接下来编写实体类
User.java
package com.s2sh.mobel;import java.io.Serializable;public class User implements Serializable{private static final long serialVersionUID = -2974230401163436730L;private Integer id;private String name;private String password;private String firstname;private String lastname;private Integer age;private User(){}private User(Integer id , String firstname , String lastname , Integer age,String name,String password){this.id = id;this.firstname = firstname;this.lastname = lastname;this.age = age;this.name=name;this.password=password;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getFirstname() {return firstname;}public void setFirstname(String firstname) {this.firstname = firstname;}public String getLastname() {return lastname;}public void setLastname(String lastname) {this.lastname = lastname;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public void setPassword(String password) {this.password = password;}public String getPassword() {return password;}public void setName(String name) {this.name = name;}public String getName() {return name;}}
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.s2sh.mobel.User" table="users"><id name="id" column="id" type="integer"><generator column="name" type="string"/><property name="password" column="password" type="string"/><property name="firstname" column="firstname" type="string" not-null="false"/><property name="lastname" column="lastname" type="string" not-null="false"/><property name="age" column="age" type="integer" not-null="false"/></class></hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. --><hibernate-configuration> <session-factory> </session-factory></hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- 数据源配置 --><bean id="dataSource" destroy-method="close"><property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property><property name="url"><value>jdbc:mysql://localhost:3306/s2sh</value></property><property name="username"><value>root</value></property><property name="password"><value>wj</value></property></bean>
<!-- sessionFactory --><bean id="sessionFactory" name="code"><!--实现层 --><bean id="userDao" scope="singleton"><property name="sessionFactory"><ref bean="sessionFactory"/></property></bean><!--service层 --><bean id="userService" ref="userDao"/></bean><!-- action --><bean id="saveUserAction" scope="prototype"><property name="userService" ref="userService" /></bean>
<!-- login --><bean id="loginUserAction" scope="prototype"><property name="userService" ref="userService" /></bean>
public void delete(User id){//获得hibernate事务 template =getHiberanteTemplate();//获得对象user =(User)template.get(User.class,id);//spring事务会进行操作删除对象template.delete(user);}
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern></filter-mapping>
<!--监听器--></filter-mapping><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!--过滤器--><filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern></filter-mapping><!--监听器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="s2sh" namespace="/" extends="struts-default" ><!-- login 用户登录--><action name="loginUser" method="login"><result name="success">/list.jsp</result><result name="error">/error.jsp</result></action></struts>
<bean id="loginUserAction" scope="prototype"><property name="userService" ref="userService" /></bean>
package com.s2sh.action.user;import java.util.Map;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.s2sh.mobel.User;import com.s2sh.service.UserService;public class UserAction extends ActionSupport {private static final long serialVersionUID = -7467352669343037994L;private User user;private UserService userService;public static long getSerialversionuid() {return serialVersionUID;}public UserService getUserService() {return userService;}private String name;private String password;public void setUserService(UserService userService) {this.userService = userService;}public User getUser() {return user;}public void setName(String name) {this.name = name;}public String getName() {return name;}public void setPassword(String password) {this.password = password;}public String getPassword() {return password;}public void setUser(User user) {this.user = user;}public String login()throws Exception{System.out.println(user.getName());if(null!=this.userService.findUser(user)){return SUCCESS;}else {return ERROR;} }
package com.s2sh.dao;import java.util.List;import com.s2sh.mobel.User;public interface UserDao {//根据用户用户来登录,不过你可以根据自己需求来写public User findUser(User user);}
package com.s2sh.dao.impl;import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.s2sh.dao.UserDao;import com.s2sh.mobel.User;public class UserDaoImpl extends HibernateDaoSupport implements UserDao{//用户登录public User findUser(User user) {try{String hql="from User where name=? and password=?";List list = (List) this.getHibernateTemplate().find(hql,new String[]{user.getName(),user.getPassword()});if(list.size()>=1){return (User)list.get(0);}else {return null;}}catch (Exception e) {e.printStackTrace();}return null;}}
package com.s2sh.service;import java.util.List;import com.s2sh.mobel.User;public interface UserService { //根据user登录public User findUser(User user);}
package com.s2sh.service.impl;import java.util.List;import com.s2sh.dao.UserDao;import com.s2sh.mobel.User;import com.s2sh.service.UserService;public class UserServiceImpl implements UserService{private UserDao userDao;//注入public void setUserDao(UserDao userDao) {this.userDao = userDao;}//用户登录public User findUser(User user){return (User)this.userDao.findUser(user);}}
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><%@ taglib uri="/struts-tags" prefix="s" %><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>用户登录</title></head><script type=text/javascript></script><script>function longin(){ var name = document.getElementById("UserName").value; var password = document.getElementById("UserPassword").value; if(name.length==""){ alert("用户名不能为空"); return false; }else if(password.length<6){ alert("密码长度不能小于6位"); return false; }else if(password.length>6){ alert("密码长度不能大于6位"); } return login.submit();}</script><body> <!-- <s:form action="loginUser" method="post"> <s:textfield name="user.name" label="name" id="username"></s:textfield> <s:textfield name="user.password" label="password" id="UserPassword"></s:textfield> <s:submit value="登录"/> </s:form> --> <form action="loginUser" name="login" method="post"> 用户名: <input type="text" name="user.name" id="UserName"/> <br/> 密码: <input type="password" name="user.password" id="UserPassword"/> <br/> <input type="button" value="登录" onclick="longin()"/> </form></body></html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><%@ taglib uri="/struts-tags" prefix="s" %><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body> ------------------>程序失败<--------------------------------</body></html>