`
yaoweinan
  • 浏览: 132747 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

Spring mvc 小编(配置)

阅读更多

上一篇讲述了Spring mvc 中常用的几个注解标示,这章中主要讲述spring mvc 的一些配置和应该注意的地方,已经试图,模板的使用。

 

首先需要在Web.xml 中添加spring 的加载器,在spring mvc 中其实你不写ContextLoaderListener 这个 跑起来基本上是没什么问题的,但是如果你要对一些Bean 的加载进行监听的话,建议你还是老老实实的加上这个类。多余的就不说了,请看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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>springmvc</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:/springContentConfig.xml</param-value>
  </context-param>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:/log4j.properties</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
 <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>
 
   <servlet>
        <servlet-name>golfing</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:/springMvcConfig.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>golfing</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>
</web-app>

 在此文件中我们定义了spring 配置的文件springContentConfig.xml,MVC Controller 的配置文件springMvcConfig.xml,如果没有指定此文件名称,spring就会按照servelt 的名称去找该配置文件。另外此xml中还配置了转码一个类以及log4j的配置文件

 

下来我们首先看一下 spring 的配置文件springContentConfig.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-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<import resource="classpath:/DBConfig.xml"/>
	<context:component-scan base-package="com.my.springmvc">
		<context:exclude-filter type="regex" expression="com.my.springmvc.web.*"/>
	</context:component-scan>
	<bean class="com.my.springmvc.util.SpringBeanInitProcesser" />
	<bean class="com.my.springmvc.util.SpringContentUtil" />
	<bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    	<property name="maxUploadSize" value="-1" />
    	<property name="defaultEncoding" value="UTF-8" />
    </bean>
      <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
      <property name="defaultEncoding" value="UTF-8"></property>
      <property name="host" value="smtp.sina.cn"/>
      <property name="username" value="****"></property>
      <property name="password" value="***"></property>
   </bean>
   <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
      <property name="velocityProperties">
         <value>
          resource.loader=class
          class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
         </value>
      </property>
   </bean>
</beans>

 此处(<import resource="classpath:/DBConfig.xml"/>)我们引用了DB的配置信息xml文件,这个后面再说。

<context:component-scan/>配置了扫描的注解的范围。

<context:exclude-filter/>配置扫描自动校验注解的范围

 <bean class="com.my.springmvc.util.SpringBeanInitProcesser" />这个配置的是监视spring 加载bean的一个类,此类需要实现接口 org.springframework.beans.factory.config.BeanPostProcessor
<bean class="com.my.springmvc.util.SpringContentUtil" />该类是一个工具类,可以自由的拿取spring 中的bean,此类需要实现接口org.springframework.context.ApplicationContextAware

下面几个就不在详解了,是配置上传文件,模板,还有邮件的一些配置。

 

下来看看我这个里面DB是怎么配置的DBConfig.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-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  <context:property-placeholder location="classpath:/jdbc.properties"/>
  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  	 <property name="driverClassName"><value>${jdbc.driver}</value></property>
     <property name="url"><value>${jdbc.url}</value></property>
     <property name="username"><value>${jdbc.username}</value></property>
     <property name="password"><value>${jdbc.password}</value></property>
  </bean>
  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  	<property name="dataSource" ref="dataSource"></property>
  </bean>
</beans>

 很简单 呵呵这个里面就是对配置文件jdbc.properties内容的设置

 

 

spring 可以说对后台的东西都over了,下面看看springMvcConfig.xml里面如何配置mvc的东西

 

<?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-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.1.xsd">
  <mvc:annotation-driven />
  <context:component-scan base-package="com.my.springmvc.web"/>
  <mvc:resources mapping="/static/**" location="/static/"/>
  <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>  
  <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
  	  <property name="defaultErrorView">
       <value>exception/exception</value>
      </property>
	   <property name="exceptionMappings">
       <props>
           <prop key="java.sql.SQLException">exception/sqlException</prop>
           <prop key="java.lang.RuntimeException">exception/runException</prop>
       </props>
       </property> 
  </bean>
  
  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="order" value="0" />
		<property name="cacheSeconds" value="0" />
		<property name="webBindingInitializer">
			<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer" >
				<property name="validator" ref="validator"/>
			</bean>
		</property>
		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>  
				<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>  
				<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>  
				<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
				</bean>
			</list>
		</property>
	</bean>
	
	<bean class="org.springframework.web.servlet.view.XmlViewResolver">
					<property name="location">
						<value>classpath:/com/my/springmvc/spring-excel-views.xml</value>
					</property>
					<property name="order" value="0"></property>
				</bean>
				
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
	    <property name="prefix" value="/views/"/>
	    <property name="suffix" value=".jsp"/>
	    <property name="order" value="1"></property>
	</bean>
</beans>

 前面几个相同的我就不说了,<mvc:resources mapping="/static/**" location="/static/"/>这个是配置一些静态资源的,这样配置之后,请求这些东西的时候spring 就不用进行过滤了。
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/> 
这个是用来校验的。

<bean id="exceptionResolver" />这个是来配置出现异常时呈现给用户的页面

<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location">
<value>classpath:/com/my/springmvc/spring-excel-views.xml</value>
</property>
<property name="order" value="0"></property>
</bean>
这个是来配置试图的,我这有两个视图的配置

 


 

<?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-3.0.xsd">
<bean id="excelSystemMenuView"
   	class="com.my.springmvc.web.view.ExcelSystemMenuView">
</bean>
<bean id="pdfSysmenuView" class="com.my.springmvc.web.view.PDFSystemMenuView"></bean>

</beans>

 

一个是Excel 的,一个是PDF的

在mvc 那个文件中还有两个比较重要的配置

 

1。<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="order" value="0" />
<property name="cacheSeconds" value="0" />
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer" >
<property name="validator" ref="validator"/><--前面配置的validate就是在这里使用的-->
   </bean>
</property>
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> 
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/> 
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/> 
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" ><!--这一堆都是配置信息转换的-->
    </bean>
</list>
</property>
</bean>

 

2。

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/views/"/>
    <property name="suffix" value=".jsp"/>
    <property name="order" value="1"></property>
</bean>
这一段是视图解析,prefix 指定以项目目录为根的相对路径,suffix 指定 视图的文件的后缀。这个你可以配置多个,在代码中具体进行调用。

分享到:
评论

相关推荐

    详解Spring mvc的web.xml配置说明

    本篇文章主要介绍了Spring mvc的web.xml配置说明,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    J2EE spring mvc mybatis bootstrap HTML5 后台框架 控制台 mysql版本_spring3.0

    【源码】mysql版本_spring3.0 系统模块 1. 组织管理:角色管理,分角色组和成员,有组权限和成员权限。 2. 系统用户:对各个基本的组会员增删改查,单发、群发邮件短信,导入导出excel表格,批量删除 3. 会员管理:...

    spring+spring mvc+hibernate开发工程财务管理辅助系统

    spring+spring mvc+hibernate+easyui+jquery+ehcache http://localhost:8080/admin/index 账号HBU001 111111 管理员admin admin 注意事项 1.系统的默认用户超级管理员:admin(密码:admin)。系统的操作:用户超级...

    spring Mvc配置xml使ResponseBody返回Json的方法示例

    主要给大家介绍了关于spring Mvc配置xml使ResponseBody返回Json的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

    详解Spring MVC事务配置

    主要介绍了详解Spring MVC事务配置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Spring MVC的web.xml配置详解

    主要介绍了Spring MVC的web.xml配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    spring mvc 读取xml文件数据库配置参数的方法

    下面小编就为大家带来一篇spring mvc 读取xml文件数据库配置参数的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    详解Spring MVC4 纯注解配置教程

    本篇文章主要介绍了Spring MVC4 纯注解配置教程,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    J2EE spring mvc mybatis bootstrap HTML5 后台框架 控制台 oracle版本_spring3.0

    【源码】oracle版本_spring3.0 系统模块 1. 组织管理:角色管理,分角色组和成员,有组权限和成员权限。 2. 系统用户:对各个基本的组会员增删改查,单发、群发邮件短信,导入导出excel表格,批量删除 3. 会员管理:...

    Spring MVC注解与数据绑定(含源码及结果图)

    1、有如下一个订单信息页面order.jsp(置于/WEB-INF/jsp目录下),按以下步骤实现一个使用POJO...(5)配置springmvc-config.xml文件。 (6)创建一个result.jsp结果页面(置于/WEB-INF/jsp目录下),用于显示接收到订单信息。

    详解如何让Spring MVC显示自定义的404 Not Found页面

    主要介绍了详解如何让Spring MVC显示自定义的404 Not Found页面,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    spring security 参考手册中文版

    38. Spring Data&Spring安全配置 273 39. @Query中的安全表达式 273 第八部分 附录 274 40.安全数据库模式 274 40.1用户模式 274 40.1.1集团当局 274 40.2持久登录(记得我)架构 275 40.3 ACL模式 275 40.3.1 ...

    Spring MVC中处理ajax请求的跨域问题与注意事项详解

    所以下面这篇文章主要给大家介绍了关于Spring MVC中处理ajax请求的跨域问题与注意事项的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。

    Spring Boot中文文档.rar

    28.1.1.Spring MVC自动配置 28.1.2.HttpMessageConverters 28.1.3.自定义JSON序列化程序和反序列化程序 28.1.4.MessageCodesResolver的信息 28.1.5.静态内容 28.1.6.欢迎页面 28.1.7.自定义Favicon ...

    clicktocall-spring:使用Spring MVC实现的点击呼叫演示应用程序

    单击以致电Spring 我们目前正在更新此样本模板。 如果您在样本中遇到任何问题,请在打开问题,我们将尽力为您提供帮助。 关于 使用Twilio实现“点击通话”的示例应用程序。 其他语言的实现: 。网 Python 节点 ...

    springmvc配置线程池Executor做多线程并发操作的代码实例

    今天小编就为大家分享一篇关于springmvc配置线程池Executor做多线程并发操作的代码实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    JavaEE开发的颠覆者+Spring+Boot实战++完整版

    5 1.3 Spring 基础配置 .....................................17 第2 章 Spring 常用配置 .... ......................... ... 30 2.1 Bean 的Scope .... ................................... 30 2.2 Spring EL ...

    基于Javaweb的jsp图书借阅系统源码(含数据库脚本).rar

    基于Spring Spring MVC MyBatis的图书馆管理系统,使用Maven进行包管理。 主要功能包括:图书查询、图书管理、图书编辑、读者管理、图书的借阅与归还以及借还日志记录等。 环境配置 开发环境:idea或eclipse 概念...

Global site tag (gtag.js) - Google Analytics