project2.ssm實現的註冊功能.2.部屬描述符(web.xml)與springMvc配置文件

1.部屬描述符(web.xml)配置

1.1url-pattern中配置/而不是/*

tomcat有自己內置的處理各種請求的servlet,若/*則會攔截.jsp結尾的請求,並將其轉發到自己內置的servlet處理器,從而使得請求失敗

1.2自定義DispatcherServlet的裝載時機

可以爲<servlet>中添加<load-on-startup>1</load-on-startup>從而設置DispatcherServlet在服務器啓動時裝載,若不寫則會在第一個HTTP請求到達時裝載

1.3自定義SpringMvc配置文件的路徑

<servlet>中寫入<init-param> <param-name>contextConfigLocation</param-name><parm-value>url</parm-value></init-param>

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>project2</display-name>
  
  <servlet>
  	<servlet-name>springMvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>springMvc</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

2SpringMvc配置文件

2.1通配符配置很全面,但無法找到annotation的解決辦法

在beans中加入這兩個配置語句意思是MVC使用http://www.springframework.org/schema/mvc/spring-mvc.xsd這個文件來解析

xmlns:mvc=“http://www.springframework.org/schema/mvc”
xsi:http://www.springframework.org/schema/mvc/spring-mvc.xsd ">

2.2在springMvc配置文件中聲明響應的編碼格式爲UTF-8

含義是爲HttpMessageConverter在執行write方法過程中調用的getMessageConverters(element,source,parserContext)的第一個參數賦值爲utf-8,HttpMessageConverter是一個用於在前端和後端交互信息時匹配數據類型的方法,其中有5個方法:canRead, read, write, canWrite, getSupportedMediaTypes

<mvc:annotation-driven>
	<mvc:message-converters>
		<bean class="org.springframework.http.converter.StringHttpMessageConverter">
			<constructor-arg index="0" value="utf-8"></constructor-arg>
		</bean>
	</mvc:message-converters>
</mvc:annotation-driven>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
<mvc:annotation-driven>
	<mvc:message-converters>
		<bean class="org.springframework.http.converter.StringHttpMessageConverter">
			<constructor-arg index="0" value="utf-8"></constructor-arg>
		</bean>
	</mvc:message-converters>
</mvc:annotation-driven>
<context:component-scan base-package="com.project2"></context:component-scan>

</beans>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章