当我的web.xml<servlet-mapping>
   <servlet-name>appServlet</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>里的url-pattern写成'/'.然后controller里的@RequestMapping("/"),这样写是可以正常访问到页面的如果我把xml里的'/'改成‘*.do’,@RequestMapping("/")改成@RequestMapping("test.do")就访问不到我的test.ftl了。这是什么情况我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
  <welcome-file-list>
    <welcome-file>test.ftl</welcome-file>
  </welcome-file-list>
</web-app>
我的appServlet-servlet.xml<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.fonsview.mymvc" />
<context:annotation-config /> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />    <beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
 <!-- 配置freeer模板 -->
<beans:bean class="org.springframework.web.servlet.view.freeer.FreeMarkerViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".ftl" />
        <beans:property name="contentType" value="text/html;charset=UTF-8"></beans:property>
        <beans:property name="requestContextAttribute" value="request" />
        <beans:property name="exposeSpringMacroHelpers" value="true" />
        <beans:property name="exposeRequestAttributes" value="true" />
        <beans:property name="exposeSessionAttributes" value="true" />
    </beans:bean>
</beans:beans>我的controller@RequestMapping("/")
public String test(Locale locale, Model model) {
List<People>peopleList=new ArrayList<People>();
        People people=new People();
        people.setName("coco");
        people.setPosition("C52");
        people.setSex("male");
        
        People people2=new People();
        people2.setName("linn");
        people2.setPosition("C53");
        people2.setSex("male");
        
        peopleList.add(people);
        peopleList.add(people2);
model.addAttribute("people", people);
model.addAttribute("peopleList", peopleList);
return "test";
}

解决方案 »

  1.   

    没用过Spring mvc,但是Spring用的还是比较多帮你顶下
      

  2.   

    LZ这个不对
      @RequestMapping("/")
        public String test(Locale locale, Model model) {
    改为:
       @RequestMapping("/test.do")
        public String test(Locale locale, Model model) {
     
      

  3.   

    LZ这个不对
      @RequestMapping("/")
      public String test(Locale locale, Model model) {
    改为:
      @RequestMapping("/test")
      public String test(Locale locale, Model model) {访问test.do
      

  4.   

    要是/*.do  访问是/test.do