基本的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,那么每個方法前都會加上這個路徑。
新聞熱點
疑難解答