ssh xml方式

来源:互联网 发布:斑马gk888t mac驱动 编辑:程序博客网 时间:2024/06/15 11:04

SSH 基础配置(XML方式)  

2012-03-23 19:38:19|  分类:SSH |  标签:ssh配置  jar  xml  mysql  |字号 订阅

本文主要主要叙述的是SSH的基本配置,采用XML方式进行依赖注入。本文只有简单流程,不涉及复杂的逻辑操作。本章的代码是经过测试能运行的,贴代码出来主要是为了读者能亲自尝试,其实本文最重要的是引用的jar截图,因为每次搭建一个开发环境,总是要从头开始添加jar,将这图截下来,便于大家和自己参考。 

项目名称:basic_xmlMyeclipse版本8.5   数据库:MySQL

Struts版本2.3.1.2Hibernate版本3.5.6Spring版本2.5.6

项目结构图


应用Jar截图

数据库名:test
数据库中有student表,结构如下:
 

TestAction.java

package 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;
}
}

IStudentService.java

package com.nrq.part1.biz;import com.nrq.part1.pojo.Student;public interface IStudentService { public void addStudent(Student student); public Student getStudent(String id);}

StudentService.java

package 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; }}

IStudentDao.java

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);}


 StudentDao.java

package 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.java

package 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; }}

Student.hbm.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.spring下的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>

config.struts下的Student.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="basicstruts2" extends="struts-default" namespace="/part1/module1"> <action name="welcome" class="testAction" method="welcome"> <result>/part1/module1/welcome.jsp</result> </action> </package></struts>

jdbc.properties

driver=com.mysql.jdbc.Driverurl=jdbc\:mysql\://localhost\:3306/testuser=rootpassword=nrqiangglinitialPoolSize=3minPoolSize=1

maxPoolSize=20maxIdleTime=60

spring.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/*"/>

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> <constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="UTF-8"/> <include file="config/struts/*"/></struts>


WEB-INF下的web.xml

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



welcome.jsp

<%@ 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}&nbsp;&nbsp;&nbsp;</span>Welcome to this test</b></center> <br> </body></html>


 最后在浏览器的地址栏输入: http://localhost:8080/basic/part1/module1/welcome即可看到页面显示“tom welcome to the test”。其中“tom”是从数据库中取出来的。
-----------------------------------------------------------------------------------------------------------------
源码下载地址:http://vdisk.weibo.com/s/3qCng
转载自http://blog.163.com/qingshui1bei@yeah/blog/static/124078678201222371738138/