views:

33

answers:

1

I trying spring 3 mvc this package is org.spring.test and code is

@Controller
@RequestMapping("/welcome")
public class WelcomeController {

private Logger logger = org.slf4j.LoggerFactory.getLogger(WelcomeController.class);


@RequestMapping(method = RequestMethod.GET)
public void welcome() {
    logger.info("Welcome!");
}
@RequestMapping("test1")
public void test1() {
    logger.info("test1!");
}
@RequestMapping("test2")
public void test2() {
    logger.info("test2!");
}
}

web.xml is:

<servlet>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/*.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

last mvc.xml is

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
<context:component-scan base-package="org.spring.test" />
<!-- Configures support for @Controllers -->
<mvc:annotation-driven />

<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

When I use "mvn jetty:run" command and write to http://localhost:8080/springdemo/welcome is

 "HTTP ERROR 404
Problem accessing /springdemo/WEB-INF/views/welcome.jsp. Reason:
 NOT_FOUND" 

Console message is :

INFO : org.spring.test.WelcomeController - Welcome!
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/springdemo/WEB-INF/views/welcome.jsp] in DispatcherServlet with name 'Spring MVC Dispatcher Servlet'

Why?

+2  A: 

It looks like you are missing /WEB-INF/views/test.jsp. Is that true?

You are requesting /test URI, which is not mapped to your controller class (which is mapped to /welcome, I think). Spring uses InternalResourceViewResolver to serve your request. It takes prefix + "test" + suffix. That is how you end up with /WEB-INF/views/test.jsp.

Make sure you have that JSP or change your controller mappings.

Update: Ok, you changed the question. But do you have /springdemo/WEB-INF/views/welcome.jsp ?

Georgy Bolyuba
Hi, this demo I copy to SpringPlugin(I create a new Springtemplate is SpringMvcProject) .
EdwardLau