在eclipse中搭建SSH框架

环境:spring3.2.0+struts2.3.20+hibernate3.6.10.Final

一、创建工程spring_struts2_hibernate,结构目录如下


二、导入spring、struts2、hibernate包以及相关jar包,如下图红色标注的jar包:



三、配置文件并编写代码,各个文件内容如下:

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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>spring_struts2_hibernate</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>
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/beans.xml</param-value>
  </context-param>  
  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>  
  <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>
</web-app>

beans.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-2.5.xsd">    
      
      <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
           destroy-method="close">
           <property name="driverClass" value="org.gjt.mm.mysql.Driver"/>
           <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ttt?characterEncoding=UTF-8"/>
           <property name="user" value="root"/>
           <property name="password" value="root"/>
           <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>com/jiaju/entity/Person.hbm.xml</value>
                </list>
           </property>           
           <property name="hibernateProperties">
                <value>
                hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
                hibernate.hbm2ddl.auto=update
                hibernate.show_sql=true
                hibernate.format_sql=true
                </value>
           </property>
      </bean>      
      <bean id="personDao" class="com.jiaju.dao.PersonDaoHibernate">
          <property name="sessionFactory" ref="sessionFactory"/>
      </bean>      
      <bean id="registService" class="com.jiaju.service.RegistServiceImpl">
          <property name="personDao" ref="personDao"/>
      </bean>
</beans>

login.jsp

<%@ 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">
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="loginPage"/></title>
</head>
<body>
<s:form action="loginPro">       
      <s:textfield name="person.username" lable="用户名" key="用户名" />
      <s:textfield name="person.password" lable="密码" key="密码" />
      <s:submit key="login" value="注册"/>                 
</s:form>    
</body>
</html>

error.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="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=GBK">
<title>errorPage</title>
</head>
<body>
   Wrong!  
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="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=GBK">
<title>successfulPage</title>
</head>
<body>
${requestScope.tip }
</body>
</html>

struts.xml

<?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="false" />
    <constant name="struts.devMode" value="true" />
    <package name="default" namespace="/" extends="struts-default">     
        <action name="loginPro" class="com.jiaju.action.RegistAction">
            <result name="success">/welcome.jsp</result>
            <result name="error">/error.jsp</result>
        </action>  
    </package>   
</struts>

RegistAction.java

package com.jiaju.action;
import com.jiaju.entity.Person;
import com.jiaju.service.RegistService;
import com.opensymphony.xwork2.Action;

public class RegistAction implements Action {
private Person person;
private String tip;
private RegistService registService;

public void setRegistService(RegistService registService){
this.registService = registService;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
public String execute() throws Exception {
if(registService.regist(person)){
setTip("哈哈,注册成功!");
return SUCCESS;
}else{
return ERROR;
}
}
}

PersonDao.java

package com.jiaju.dao;
import com.jiaju.entity.Person;

public interface PersonDao {
public Integer save(Person person);
}

PersonDaoHibernate.java

package com.jiaju.dao;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.jiaju.entity.Person;

public class PersonDaoHibernate extends HibernateDaoSupport implements PersonDao {
public Integer save(Person person){
return (Integer)getHibernateTemplate().save(person);
}
}

Person.java

package com.jiaju.entity;

public class Person {
private int id;
private String username;
private String password;

public int getId() {
return id;
}
public void setId(int 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;
}
}

Person.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 package="com.jiaju.entity">
     <class name="Person" table="person">
         <id name="id">
             <generator class="native"/>
         </id>
         <property name="username"/>
         <property name="password"/>
     </class>
</hibernate-mapping>

RegistService.java

package com.jiaju.service;
import com.jiaju.entity.Person;

public interface RegistService {
public boolean regist(Person person);
}

RegistServiceImpl.java

package com.jiaju.service;
import com.jiaju.dao.PersonDao;
import com.jiaju.entity.Person;

public class RegistServiceImpl implements RegistService {
private PersonDao personDao;

public void setPersonDao(PersonDao personDao){
this.personDao = personDao;
}

@Override
public boolean regist(Person person) {
int result = personDao.save(person);
if(result > 0){
return true;
}
return false;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值