Struts2_Hibernate 3.3_Spring 3.0 配置

配置文件
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">

<!-- filter -->
<filter>
<filter-name>charFilter</filter-name>
<filter-class>extfilter.CharFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>charFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- ++++++++++++ -->
<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>*.action</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.js</url-pattern>
</filter-mapping>

<!-- listener -->
<listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<!-- param -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
\WEB-INF\classes\applicationContext.xml
</param-value>
</context-param>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

<package name="ssh" extends="struts-default">

<global-results>
    <result name="toListUserinfoAction"type="redirectAction">
    listUserinfo.action
    </result>
</global-results>

<action name="baseController" class="baseController"></action>

<!-- 添加用户信息 -->
<action name="insertUserinfo" class="insertUserinfo"></action>

<!-- 显示用户信息 -->
<action name="listUserinfo" class="listUserinfo">
<result name="listusrinfoJSP">/listUserinfo.jsp</result>
</action>

    </package>

</struts>    

controller.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" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


    <bean id="baseController" class="controller.BaseController">
        <property name="allService">
            <ref bean="allService" />
        </property>
    </bean>

    <bean id="insertUserinfo" class="controller.InsertUserinfo"
        parent="baseController" scope="prototype">
    </bean>

    <bean id="listUserinfo" class="controller.ListUserinfo" parent="baseController" scope="prototype">
    </bean>

</beans>

service.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" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="userinfoService" class="service.UserinfoService">
        <property name="allDao">
            <ref bean="allDao" />
        </property>
    </bean>

    <bean id="allService" class="service.AllService">
        <property name="userinfoService">
            <ref bean="userinfoService" />
        </property>
    </bean>

</beans>

dao.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" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">



    <bean id="UserinfoDAO" class="orm.UserinfoDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

    <bean id="UserinfoDAOExt" class="extorm.UserinfoDAOExt">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

    <bean id="allDao" class="orm.AllDao">
        <property name="userinfoDAO">
            <ref bean="UserinfoDAO" />
        </property>

        <property name="userinfoDAOExt">
            <ref bean="UserinfoDAOExt" />
        </property>
    </bean>

</beans>

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" 

    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"

    xsi:schemaLocation="

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

            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

    ">

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml">
        </property>
    </bean>

    <!-- 添加事务 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>

    <!-- 数据源 -->
    <import resource="controller.xml"></import>
    <import resource="dao.xml"></import>
    <import resource="service.xml"></import>

</beans>

框架结构
Service包

package service;

public class AllService {

    private UserinfoService userinfoService;

    public UserinfoService getUserinfoService() {
        return userinfoService;
    }

    public void setUserinfoService(UserinfoService userinfoService) {
        this.userinfoService = userinfoService;
    }

}
package service;

import java.util.List;
import orm.AllDao;
import orm.Userinfo;

public class UserinfoService {

    private AllDao allDao;

    public AllDao getAllDao() {
        return allDao;
    }

    public void setAllDao(AllDao allDao) {
        this.allDao = allDao;
    }

    public void insertUserinfo(String username, String password) {

        Userinfo userinfo = new Userinfo();
        userinfo.setUsername(username);
        userinfo.setPassword(password);
        this.getAllDao().getUserinfoDAO().save(userinfo);
    }

    public List getUserinfoAll() {

        return this.getAllDao().getUserinfoDAOExt().queryUserinfoAll();
    }

}

controller包

package controller;

import service.AllService;

public class BaseController {

    /*
     * 注意:在使用SSH整合项目中(在使用Myeclipse8.5的情况下),建议不要在WEB项目使用Myeclipse中的功能,向项目中添加STRUTS2
     * ,要使用手动方式添加STRUTS2框架, 如果用Myeclipse工具添加STRUTS2框架,启动时会报异常,原因就是Jar包冲突!
     */

    private AllService allService;

    public AllService getAllService() {
        return allService;
    }

    public void setAllService(AllService allService) {
        this.allService = allService;
    }

}
package controller;

import org.springframework.transaction.annotation.Transactional;

@Transactional
public class InsertUserinfo extends BaseController {

    private String username;
    private String password;

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

    public String execute() {
        this.getAllService().getUserinfoService().insertUserinfo(username,
                password);
        return "toListUserinfoAction";
    }

}
package controller;

import java.util.List;

public class ListUserinfo extends BaseController {

    private List listUserinfo;

    public List getListUserinfo() {
        return listUserinfo;
    }

    public void setListUserinfo(List listUserinfo) {
        this.listUserinfo = listUserinfo;
    }

    public String execute() {

        listUserinfo = this.getAllService().getUserinfoService()
                .getUserinfoAll();

        System.out.println("+++++++++++++++++++++++:" + listUserinfo.size());
        return "listusrinfoJSP";
    }

}

扩展extorm包

package extorm;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class UserinfoDAOExt extends HibernateDaoSupport {

    public List queryUserinfoAll() {
        return this.getSession().createQuery(
                "from Userinfo u order by u.id asc").list();
    }

}

扩展extfilter包

package extfilter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class CharFilter implements Filter {

    public void destroy() {
        // TODO Auto-generated method stub

    }

    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {

        arg0.setCharacterEncoding("utf-8");
        arg1.setCharacterEncoding("utf-8");
        arg2.doFilter(arg0, arg1);
    }

    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub

    }

}

Jar文件

扫描二维码关注公众号,回复: 2435162 查看本文章

这里写图片描述

使用Myeclipse中的功能添加框架

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

猜你喜欢

转载自blog.csdn.net/u013101178/article/details/44917701