基本的web.xml配置:
<servlet> <servlet-name>sPRingDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置Spring mvc下的配置文件的位置和名称 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:springmvc.xml <!-- /WEB-INF/[servlet-name]-servlet.xml --> </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>把springMVC的类DispatcherServlet关联到路径:
<servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>这里表示所有路径。 然后配置参数:springMVC的配置文件/WEB-INF/[servlet-name]-servlet.xml。
表示加载次序。
然后在对应的路径下新建配置文件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.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置自动扫描的包 --> <context:component-scan base-package="com.jackie.spring"></context:component-scan> <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean></beans>context:component-scan表示上下文容器扫描路径,对于controller的requestmapping的映射关系的查找。 InternalResourceViewResolver表示视图路径,属性有前缀prefix和后缀suffix,到时候controller的返回值String类型加入就成了一个视图的路径,然后再去渲染。
如果在类前加上requestmapping,那么每个方法前都会加上这个路径。
新闻热点
疑难解答