SSH 基础配置(XML方式)
2012-03-23 19:38:19| 分类: SSH | 标签:ssh配置 jar xml mysql |字号大中小 订阅
项目名称:basic_xml Myeclipse版本:8.5 数据库:MySQL
Struts版本:2.3.1.2 Hibernate版本:3.5.6 Spring版本:2.5.6
项目结构图
![](https://img-my.csdn.net/uploads/201205/25/1337907812_6269.jpg)
![](https://img-my.csdn.net/uploads/201205/25/1337907812_6269.jpg)
![](https://img-my.csdn.net/uploads/201205/25/1337907890_8176.jpg)
![](https://img-my.csdn.net/uploads/201205/25/1337907905_2059.jpg)
IStudentService.javapackage com.nrq.part1.action;
import com.nrq.part1.biz.IStudentService;import com.nrq.part1.pojo.Student;import com.opensymphony.xwork2.ActionSupport;public class TestAction extends ActionSupport{private static final long serialVersionUID = 1L;private IStudentService studentService;private Student student;public String welcome(){this.student = studentService.getStudent("1001");return SUCCESS;}//-----getter、setter------public IStudentService getStudentService() {return studentService;}public void setStudentService(IStudentService studentService) {this.studentService = studentService;}public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}}
StudentService.javapackage com.nrq.part1.biz; import com.nrq.part1.pojo.Student; public interface IStudentService { public void addStudent(Student student); public Student getStudent(String id); }
IStudentDao.javapackage com.nrq.part1.biz.impl; import com.nrq.part1.biz.IStudentService; import com.nrq.part1.dao.IStudentDao; import com.nrq.part1.pojo.Student; public class StudentService implements IStudentService{ private IStudentDao studentDao; public void addStudent(Student student) { studentDao.saveStudent(student); } public Student getStudent(String id) { return studentDao.getStudent(id); } //------getter、setter----- public IStudentDao getStudentDao() { return studentDao; } public void setStudentDao(IStudentDao studentDao) { this.studentDao = studentDao; } }
package com.nrq.part1.dao; import java.io.Serializable; import com.nrq.part1.pojo.Student; public interface IStudentDao { public void saveStudent(Student student); public Student getStudent(Serializable id); }
Student.javapackage com.nrq.part1.dao.impl; import java.io.Serializable; import org.springframework.orm.hibernate3.HibernateTemplate; import com.nrq.part1.dao.IStudentDao; import com.nrq.part1.pojo.Student; public class StudentDao implements IStudentDao{ HibernateTemplate hibernateTemplate; public Student getStudent(Serializable id) { return (Student)hibernateTemplate.get(Student.class, id); } public void saveStudent(Student student) { hibernateTemplate.save(student); } //---getter,setter--- public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } }
Student.hbm.xmlpackage com.nrq.part1.pojo; public class Student { private String id; private String name; private String gender; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } }
config.spring下的Student.xml<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.nrq.part1.pojo"> <class name="Student" table="student"> <id name="id"> <column name="id" length="32"></column> <generator class="uuid.hex"></generator> </id> <property name="name"/> <property name="gender"/> </class> </hibernate-mapping>
config.struts下的Student.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="testAction" class="com.nrq.part1.action.TestAction"> <property name="studentService" ref="studentService"></property> </bean> <bean id="studentService" class="com.nrq.part1.biz.impl.StudentService"> <property name="studentDao" ref="studentDao"></property> </bean> <bean id="studentDao" class="com.nrq.part1.dao.impl.StudentDao"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> </beans>
jdbc.properties<?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="basicstruts2" extends="struts-default" namespace="/part1/module1"> <action name="welcome" class="testAction" method="welcome"> <result>/part1/module1/welcome.jsp</result> </action> </package> </struts>
spring.xmldriver=com.mysql.jdbc.Driver url=jdbc\:mysql\://localhost\:3306/test user=root password=nrqianggl initialPoolSize=3 minPoolSize=1
maxPoolSize=20 maxIdleTime=60
struts.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="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:jdbc.properties</value> </list> </property> </bean> <!-- 配置数据源 c3p0池连接--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${driver}" /> <property name="jdbcUrl" value="${url}" /> <property name="user" value="${user}" /> <property name="password" value="${password}" /> <property name="initialPoolSize" value="${initialPoolSize}" /> <property name="minPoolSize" value="${minPoolSize}" /> <property name="maxPoolSize" value="${maxPoolSize}" /> <property name="maxIdleTime" value="${maxIdleTime}" /> </bean> <!-- 配置sessionFactory、hibernate属性 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--使用mappingDirectoryLocations指定某目录下的hbm文件--> <property name="mappingDirectoryLocations"> <list> <value>classpath*:config/hbm</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="connection.useUnicode">true</prop> <prop key="connection.characterEncoding">UTF-8</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> </props> </property> </bean> <!-- HIbernate模板 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 将config/spring下的xml导入 --> <import resource="classpath*:config/spring/*"/>
<?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> <constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="UTF-8"/> <include file="config/struts/*"/> </struts>
<?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"> <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> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring.xml</param-value> </context-param> </web-app>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>Welcome to this test</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <center><b><span style="color:red;font-size:25px;">${student.name} </span>Welcome to this test</b></center> <br> </body> </html>
最后在浏览器的地址栏输入: http://localhost:8080/basic/part1/module1/welcome 即可看到页面显示“tom welcome to the test”。其中“tom”是从数据库中取出来的。