Spring+hibernate基本配置
2016-02-23 22:28
357 查看
web.xml
application-context.xml
Servlet-conf.xml
<?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>WebChart</display-name> <filter> <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/application-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/Servlet-*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <session-config> <session-timeout>1</session-timeout> </session-config> </web-app>
application-context.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:context="http://www.springframework.org/schema/context" 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.xsd"> <context:component-scan base-package="com.zhey.webchart"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"> <property name="driverClassName"> <value>org.postgresql.Driver</value> </property> <property name="url"> <value>jdbc:postgresql://localhost:5432/dbtmp</value> </property> <property name="username"> <value>username</value> </property> <property name="password"> <value>password</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"> </property> <property name="hibernateProperties"> <props> <!-- 指定数据库方言 --> <prop key="hibernate.dialect"> org.hibernate.dialect.PostgreSQLDialect </prop> <!-- 是否根据需要每次自动创建数据库 --> <prop key="hibernate.hbm2ddl.auto">update</prop> <!-- 显示Hibernate持久化操作所生成的SQL --> <prop key="hibernate.show_sql">true</prop> <!-- 将SQL脚本进行格式化后再输出 --> <prop key="hibernate.format_sql">true</prop> </props> </property> <!-- 通过注解的方式获取pojo的配置 --> <property name="annotatedClasses"> <list> <value>com.zhey.chart.po.User</value> </list> </property> </bean> </beans>
Servlet-conf.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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <context:component-scan base-package="com.zhey.webchart.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <!-- 指定StringHttpMessageConverter构造函数的参数, StringHttpMessageConverter默认的编码方式是ISO-8859-1 --> <constructor-arg> <bean class="java.nio.charset.Charset" factory-method="forName"> <constructor-arg value="UTF-8" /> </bean> </constructor-arg> </bean> </mvc:message-converters> </mvc:annotation-driven> <mvc:resources mapping="/js/**" location="/WEB-INF/js/" /> <bean name="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <bean class="org.springframework.http.MediaType"> <constructor-arg value="text" /> <constructor-arg value="plain" /> <constructor-arg value="UTF-8" /> </bean> </list> </property> </bean> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/" /> <property name="suffix" value=".jsp" /> <property name="contentType" value="text/html;charset=UTF-8" /> <property name="order" value="1" /> </bean> </beans>
相关文章推荐
- JAVA_构造函数重载 OverloadingConstructors.java
- JAVA_构造函数重载 OverloadingConstructors.java
- Java EE 工程师系列 之 从后台到页面【最佳实践】
- Java基础I/O流总结
- JAVA正则表达式语法大全
- java正则表达式相关内容整理
- java的拆箱与装箱
- win10 配置 java8
- 国内开源 java cms,Jspxcms 6.0.1 发布
- Java.concurrent.locks(2)-ReentrantLock
- 使用eclipse 做嵌入式开发
- 【慕课笔记】第四章 JAVA中的集合框架(上) 第9节 学生选课—应用泛型管理课程(一)
- struts2拦截器的实现原理
- 根据一份web.xml文件来详解Servlet
- Java 反射修改 final 属性值
- Java 中读写文件内容常见的几种方法
- 我的笔记:精通JAVA集合类
- Eclipse 快捷键大全
- java设计模式之策略模式
- JDK5并发(2) Locks-ReentrantLock