您的位置:首页 > 编程语言 > Java开发

Spring MVC+Spring+hibernate框架配置整合

2015-04-03 18:03 363 查看
新建cp网站项目

1、把包导入到cp\WebContent\WEB-INF\lib

2、在eclipse中,把需要的jar包粘贴到WEB-INF/lib下,会自动导入Libraries/web app libraries

3、修改web.xml,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd"> <display-name>cp</display-name>

<!-- 配置spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<listener>
<!-- 加载spring配置文件 -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- end 配置spring -->

<!-- springMVC配置 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- end springMVC配置 -->

<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

<!-- 检查是否登录过滤器 -->
<!-- <filter>
<filter-name>CheckLoginFilter</filter-name>
<filter-class>com.winheng.util.CheckLoginFilter</filter-class>
<init-param>
<param-name>exceptUrls</param-name>
<param-value>
.jpg,.png,.gif,.js,.css,/Index/index

</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>CheckLoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->

</web-app>


4、在src下新建springmvc-servlet.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">
<!-- <context:annotation-config />-->
<!-- 激活Spring注解方式:自动扫描,并注入bean -->
<context:component-scan base-package="com.icap.web.controller" />
<!-- end 激活Spring注解方式:自动扫描,并注入bean -->

<!-- 开启注解 -->
<!-- 根据类找方法 -->
<!--<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />-->
<!-- 通过url找到类 -->
<!--<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />-->
<!-- 可用  <mvc:annotation-driven /> 代替上面两个配置-->
<mvc:annotation-driven />
<!-- end 开启注解 -->

<!-- 静态资源访问   过滤资源请求-->
<!-- 必须先开启注解 -->
<mvc:resources location="/resources/" mapping="/resources/**" />
<mvc:resources location="/back/" mapping="/back/**" />
<mvc:resources location="/umeditor/" mapping="/umeditor/**" />
<!--end 静态资源访问   过滤资源请求-->

<!-- 配置视图解析 -->

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="viewClass"  value="org.springframework.web.servlet.view.JstlView" />
</bean>
<!-- 文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- set the max upload size100MB -->
<property name="maxUploadSize">
<value>104857600</value>
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>

</beans>


5、在src下新建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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
" default-autowire="byName">

<!-- 导入外部文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />

<!-- 配置dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

<!-- 	 配置sessionFactory  -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
<property name="packagesToScan" value="com.icap.domain"></property>
</bean>
<!-- end 整合hibernate -->

<!-- 配置Hibernate事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager" autowire="byName">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 基于注释的事务,当注释中发现@Transactional时,使用id为“transactionManager”的事务管理器  -->
<!-- 如果没有设置transaction-manager的值,则spring以缺省默认的事务管理器来处理事务,默认事务管理器为第一个加载的事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- end 配置Hibernate事务管理器 -->

<bean id="memberDaoImpl" class="com.icap.dao.impl.MemberDaoImpl"></bean>

<!--<bean id="userService" class="com.winheng.service.impl.UserServiceImpl"></bean>  -->

<!-- 激活Spring注解方式:自动扫描,并注入bean -->
<!-- <context:component-scan base-package="com.icap.dao" />-->
<!--<context:component-scan base-package="com.icap.service" />-->

</beans>


6、建立com.winheng.web.controller包,在下面建立IndexConterller.java,内容参考本目录下IndexConterller.java

package com.icap.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value="/Index")
public class IndexController {

@RequestMapping(value="/index")
public ModelAndView index(){
ModelAndView mav = new ModelAndView();
mav.setViewName("/index/index");

System.out.println("index/inde");
return mav;
}
}


7、在WEB-INF下简历jsp文件夹,简历index.jsp视图

8、访问localhost:8080/cp/Index/index 成功~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐