ITeye博客

struts2+spring3+hibernate3整合

X_boy · 2011-10-04 00:56 · J2EE · 编程语言 · SSH · struts2+spring3+hibernate3整合 · Markdown

网上看了很多例子,ssh整合都没有往数据库中插入记录,比如像注册这样的例子。刚接触ssh整合,写得不好。我用的是eclipse for j2ee,先上图看看整体架构

 

ssh

 

1、LoginAction.java

 

 

package action;

 

import impl.MyServiceImpl;

 

import com.opensymphony.xwork2.Action;

 

import model.User;

 

public class LoginAction implements Action{

private User user;

private MyServiceImpl ms;

private String tip;

public User getUser() {

return user;

}

public void setUser(User user) {

this.user = user;

}

public MyServiceImpl getMs() {

return ms;

}

public void setMs(MyServiceImpl ms) {

this.ms = ms;

}

public String getTip() {

return tip;

}

public void setTip(String tip) {

this.tip = tip;

}

public String execute()throws Exception

    {      

  ms.regedit(user);

            if (ms.valid(user.getUsername(),user.getPassword()))

            {

                 setTip("呵,整合成功!");

                return SUCCESS;

             }

            else

                return ERROR;

 

     }

 

}

2、MyService.java

package impl;

public interface MyService {

public boolean valid(String username , String password);

}

3、MyServiceImpl.java

package impl;

import model.User;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.orm.hibernate3.HibernateTemplate;

public class MyServiceImpl  implements MyService{

//处理注册

public void regedit(User user){

ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

HibernateTemplate ht=(HibernateTemplate)context.getBean("hibernateTemplate");

      

ht.save(user);

}

//处理验证

public boolean valid(String username , String password){

if (username.equals("hello") && password.equals("world"))

        {

            return true;

         }

        else

            return false;

    

}

}

4、User.java

package model;

public class User {

private Integer id;

private String username;

private String password;

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;

}

}

5、User.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<!-- Used to demonstrate the declarative configuration of both hbm files 

and annotated classes See hibernate.cfg.xml and ConfigurationTest -->

<hibernate-mapping package="model">

<class name="User" table="user_table">

<id name="id">

<generator class="identity" />

</id>

<property name="username" />

<property name="password" />

</class>

</hibernate-mapping>

6、bean.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 

"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close">

<property name="driverClass" value="com.mysql.jdbc.Driver" />

<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8" />  

<property name="user" value="root" />

<property name="password" value="mysql" />

<property name="maxPoolSize" value="40" />

<property name="minPoolSize" value="1" />

<property name="initialPoolSize" value="1" />

<property name="maxIdleTime" value="20" />

</bean>

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="mappingResources">

<list>

<value>model/User.hbm.xml</value>

</list>

</property>

<property name="hibernateProperties">

<value>

hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect

hibernate.hbm2ddl.auto=update

hibernate.shou_sql=true

hibernate.format_sql=true

</value>

</property>

</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">

<property name="sessionFactory" ref="sessionFactory"/>

</bean>

</beans>

7、struts.xml

<?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="default" namespace="/" extends="struts-default">

        <action name="login" class="action.LoginAction">

             <result name="success">/success.jsp</result>

             <result name="error">/error.jsp</result>

        </action>

    </package>

</struts>

8、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

       http://www.springframework.org/schema/beans/spring-beans.xsd">

             

<bean id="ms" class="impl.MyServiceImpl" />

</beans>

9、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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <display-name>ssh</display-name>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  

  <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>

10、index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>welcome login</title>

</head>

<body>

<s:form action="login" method="post">

<s:textfield name="user.username" label="用户名" />

<s:textfield name="user.password" label="密码" />

<s:submit value="登录" />

</s:form>

</body>

</html>

11、success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>success</title>

</head>

<body>

<br>

<s:property value="tip"/><br>

<s:property value="user.username"/>

</body>

</html>

12、error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>error</title>

</head>

<body>

哎~登录失败!!!

</body>

</html>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。