目录结构如下:
自己写的web.xml
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring MVC Application</display-name> <servlet> <servlet-name>HelloWeb</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:HelloWeb-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>HelloWeb</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>Test</welcome-file> </welcome-file-list> </web-app> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:HelloWeb-servlet.xml</param-value> </init-param> 是指定自己的Spring配置的xml.问题:
对于不指定的条件下,默认是就是[serveletname]-servlet.xml默认路径是webcontent文件夹下面和web.xml同级就可以。但是有的时候会爆出不能找到[servletname]-servlet.xml;那么把该xml文件移到src文件夹下面就可以了。---- 这个问题暂时还没有研究。
<init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:HelloWeb-servlet.xml</param-value> </init-param> 这个是配置请求文件的跳转,也就是当有请求的时候直接跳转Test,因为你的spring控制器配置如下:
MainController.ava代码:
package web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller //@RequestMapping("/Web") public class MainController { @RequestMapping(value="/Test", method=RequestMethod.GET) public String returnJSP(){ return "MyWeb"; //JSP 名字 } } 控制器会将请求跳转到Test,这也是问什么配置: <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:HelloWeb-servlet.xml</param-value> </init-param>同时还需要添加一个Test空文件在webcontent下:不添加会爆出404错误!!!!!就是自己制定请求的url ---> http://localhost:8080/twoDynamic/Web/Test
但是添加了空的Test文件:
不用使用全的url便可以请求。但是反而输入全的url爆出了404:
这个原因可能:在spring控制器中已经添加了跳转,所以没有必要请输入全url。
HelloWeb-servlet.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-4.0.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="web.controller"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>