Spring mvc小编(配置)

14953 ワード

前回はSpring mvcでよく使われているいくつかの注釈表示について述べましたが、この章では主にspring mvcのいくつかの構成と注意すべきところについて説明し、テンプレートの使用を試みました.
 
まずWeb.xmlにspringのキャリアを追加する必要があります.spring mvcではContinxt Loader Listenerを書かないで走るのは基本的には大丈夫ですが、Benのロードを監視するなら、正直にこのタイプを加えることをお勧めします.余計なことは言わないで、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構成のファイルを定義しました.spring ContentConfig.xml、MVC Controllerのプロファイルspring MvcConfig.xmlは、このファイル名が指定されていない場合、springはserveltの名前に従って配置ファイルを探します.また、このxmlにはトランスコードクラスとlog 4 jのプロファイルが配置されています.
 
まず、springのプロファイルを見ます.spring ContentConfig.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>
 ここではDBの設定情報xmlファイルを引用しました.この後にまた話します.
<context:component-scan/>スキャンの注釈の範囲を設定しました.
<context:exclude-filter/>スキャン自動チェックの範囲を設定します.
 以下のいくつかは詳細には解りません.アップロードファイル、テンプレート、メールのいくつかの構成を配置しています.
 
この中の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>
 前のいくつかの同じものは言いません.これはいくつかの静的な資源を配置しています.このように配置してから、これらのものを要求するときはspringはフィルタリングしなくてもいいです.bean id=「validator」class=「org.spring frame ebook.validation.beanvalidations.LocalValidator FactoryBean」/」  これは検証用です.
<bean id=「exception Resolover」/>これは、異常が発生した場合にユーザーに表示されるページを設定します.
<bean class=「org.spring frame ework.web.servlet.view.Xml View Resolover」><property name=「location」<value>classpath:/com/myl/springmvc/spring-excel-views.xmls.xml<propernaty>の配置を試みました.
 
 
<?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.sprigfraamewaork.web.servlet.mvc.annotatition.AnnotatitionAnAnnotatitinMethodhandlerAdapter""<--前に配置されているvalidateはここで使われています.  
     
 
2です
<bean id=「view Resolover」class=「org.spring frame ework.web.servlet.view.InternalResource View Resoliver」>       
この段落はビュー解析であり、prefixはプロジェクトディレクトリをルートとする相対パスを指定し、suffixはビューのファイルのサフィックスを指定します.これは複数の設定ができます.コードで具体的に呼び出します.