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