1.项目结构(jar包过多):
2.代码:
1.web.xml:配置servlet和spring容器
<?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>SSH</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>
<!-- 加载spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 加载spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 注册Struts1服务 -->
<servlet>
<servlet-name>struts</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>struts</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
<?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>SSH</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>
<!-- 加载spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 加载spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 注册Struts1服务 -->
<servlet>
<servlet-name>struts</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>struts</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
2.struts-config.xml:servlet配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd ">
<struts-config>
<!-- <action-mappings>
<action path="/person/list" type="com.ssh.web.action.PersonAction" validate="false">
<forward name="list" path="/WEB-INF/page/personlist.jsp"></forward>
</action>
</action-mappings> -->
<!-- 上下两部分功能相同,下面这个使用spring的bean 中 name="/person/list" 的bean处理 -->
<action-mappings>
<action path="/person/list" validate = "false">
<forward name="list" path="/WEB-INF/page/personlist.jsp"></forward>
</action>
</action-mappings>
<!-- 将控制权交给spring -->
<controller>
<set-property property="processorClass"
value="org.springframework.web.struts.DelegatingRequestProcessor" />
</controller>
</struts-config>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd ">
<struts-config>
<!-- <action-mappings>
<action path="/person/list" type="com.ssh.web.action.PersonAction" validate="false">
<forward name="list" path="/WEB-INF/page/personlist.jsp"></forward>
</action>
</action-mappings> -->
<!-- 上下两部分功能相同,下面这个使用spring的bean 中 name="/person/list" 的bean处理 -->
<action-mappings>
<action path="/person/list" validate = "false">
<forward name="list" path="/WEB-INF/page/personlist.jsp"></forward>
</action>
</action-mappings>
<!-- 将控制权交给spring -->
<controller>
<set-property property="processorClass"
value="org.springframework.web.struts.DelegatingRequestProcessor" />
</controller>
</struts-config>
3.beans.xml:spirng配置文件:主要配置javabean,hibernate配置,数据源配置,事务配置,sessionfactory配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 注解驱动 -->
<context:annotation-config></context:annotation-config>
<!-- 数据库配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=UTF-8"></property>
<!-- <property name="connection.url">
<value><![CDATA[jdbc:mysql:///jdbc?useUnicode=true&characterEncoding=UTF-8]]></value>
</property> -->
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="initialSize" value="1"></property>
<property name="maxActive" value="500"></property>
<property name="maxIdle" value="2"></property>
<property name="minIdle" value="1"></property>
</bean>
<!-- sessionfactory管理配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- <property name="connection.characterEncoding" >utf-8</property> -->
<!-- hibernate映射文件 多个 -->
<property name="mappingResources">
<list>
<value>com/ssh/bean/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
</bean>
<!-- hibernate事务管理bean -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- @Transactional的方式来配置声明式事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="personService" class="com.ssh.service.impl.PersonServiceImpl"></bean>
<bean name="/person/list" class="com.ssh.web.action.PersonAction" />
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 注解驱动 -->
<context:annotation-config></context:annotation-config>
<!-- 数据库配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=UTF-8"></property>
<!-- <property name="connection.url">
<value><![CDATA[jdbc:mysql:///jdbc?useUnicode=true&characterEncoding=UTF-8]]></value>
</property> -->
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="initialSize" value="1"></property>
<property name="maxActive" value="500"></property>
<property name="maxIdle" value="2"></property>
<property name="minIdle" value="1"></property>
</bean>
<!-- sessionfactory管理配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- <property name="connection.characterEncoding" >utf-8</property> -->
<!-- hibernate映射文件 多个 -->
<property name="mappingResources">
<list>
<value>com/ssh/bean/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
</bean>
<!-- hibernate事务管理bean -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- @Transactional的方式来配置声明式事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="personService" class="com.ssh.service.impl.PersonServiceImpl"></bean>
<bean name="/person/list" class="com.ssh.web.action.PersonAction" />
</beans>
4.Person.java,Person.hbm.xml:
package com.ssh.bean;
public class Person {
private Integer id;
private String name;
public Person(){}
public Person(String name){
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<?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.ssh.bean">
<class name="Person" table="person" >
<id name="id" column="id" type="int">
<generator class="native"></generator>
</id>
<property name="name" type="string" not-null="true" column="name" > </property>
</class>
</hibernate-mapping>
package com.ssh.bean;
public class Person {
private Integer id;
private String name;
public Person(){}
public Person(String name){
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<?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.ssh.bean">
<class name="Person" table="person" >
<id name="id" column="id" type="int">
<generator class="native"></generator>
</id>
<property name="name" type="string" not-null="true" column="name" > </property>
</class>
</hibernate-mapping>
5.Personservice.java,PersonServiceImpl.java:使用注解
package com.ssh.service;
import java.util.List;
import com.ssh.bean.Person;
public interface Personservice {
void save(Person person);
void update(Person person);
Person getPerson(Integer personid);
void delete(Integer personid);
List<Person> getPersons();
}
package com.ssh.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.ssh.bean.Person;
import com.ssh.service.Personservice;
@Transactional
public class PersonServiceImpl implements Personservice{
//ByName 注入
@Resource private SessionFactory sessionFactory;
@Override
public void save(Person person) {
sessionFactory.getCurrentSession().persist(person);
}
@Override
public void update(Person person) {
sessionFactory.getCurrentSession().merge(person);
}
//不开启事务?---
@Transactional(propagation=Propagation.NOT_SUPPORTED, readOnly=true)
@Override
public Person getPerson(Integer personid) {
return (Person) sessionFactory.getCurrentSession().get(Person.class, personid);
}
@Override
public void delete(Integer personid) {
sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().load(Person.class, personid));
}
//不开启事务?---
@Transactional(propagation=Propagation.NOT_SUPPORTED, readOnly=true)
@SuppressWarnings("unchecked")
@Override
public List<Person> getPersons() {
return sessionFactory.getCurrentSession().createQuery("from Person").list();
}
}
package com.ssh.service;
import java.util.List;
import com.ssh.bean.Person;
public interface Personservice {
void save(Person person);
void update(Person person);
Person getPerson(Integer personid);
void delete(Integer personid);
List<Person> getPersons();
}
package com.ssh.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.ssh.bean.Person;
import com.ssh.service.Personservice;
@Transactional
public class PersonServiceImpl implements Personservice{
//ByName 注入
@Resource private SessionFactory sessionFactory;
@Override
public void save(Person person) {
sessionFactory.getCurrentSession().persist(person);
}
@Override
public void update(Person person) {
sessionFactory.getCurrentSession().merge(person);
}
//不开启事务?---
@Transactional(propagation=Propagation.NOT_SUPPORTED, readOnly=true)
@Override
public Person getPerson(Integer personid) {
return (Person) sessionFactory.getCurrentSession().get(Person.class, personid);
}
@Override
public void delete(Integer personid) {
sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().load(Person.class, personid));
}
//不开启事务?---
@Transactional(propagation=Propagation.NOT_SUPPORTED, readOnly=true)
@SuppressWarnings("unchecked")
@Override
public List<Person> getPersons() {
return sessionFactory.getCurrentSession().createQuery("from Person").list();
}
}
6.PersonAction.java:处理请求service层:
package com.ssh.web.action;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.ssh.bean.Person;
import com.ssh.service.Personservice;
public class PersonAction extends Action{
@Resource Personservice personService;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
//获得spring容器实例
/*WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());
Personservice personService = (Personservice) ctx.getBean("personService");*/
request.setAttribute("persons", personService.getPersons());
for (Person p : personService.getPersons()) {
System.out.println(""+p.getId() + " "+p.getName());
}
return mapping.findForward("list");
}
}
package com.ssh.web.action;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.ssh.bean.Person;
import com.ssh.service.Personservice;
public class PersonAction extends Action{
@Resource Personservice personService;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
//获得spring容器实例
/*WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());
Personservice personService = (Personservice) ctx.getBean("personService");*/
request.setAttribute("persons", personService.getPersons());
for (Person p : personService.getPersons()) {
System.out.println(""+p.getId() + " "+p.getName());
}
return mapping.findForward("list");
}
}
3.总结:
1.配置web.xml, struts-servlet.xml,beans.xml(最主要的配置)
2.处理业务逻辑
4.tips:
@Resource装配顺序
1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;
1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;