无法获得第二个在MVS中运行的Spring MVC应用程序。

时间:2012-01-03 20:04:18

标签: spring-mvc mapping

我正在使用本教程:

http://blog.springsource.org/2011/01/04/green-beans-getting-started-with-spring-mvc/

使用spring的工具套件3.0(sts)运行Spring MVC应用程序。

当部署一个应用程序(BareMvc)并使用此URL时,它可以正常工作:

http://localhost:8080/BareMvc/

但是,如果我尝试部署第二个(AnotherMvcProject),请使用此URL:

http://localhost:8080/AnotherMvcProject/

它返回404找不到页面错误,控制台显示以下错误消息:

ARN:org.springframework.web.servlet.PageNotFound - 在名为'appServlet'的DispatcherServlet中找不到带有URI [/ AnotherMvcProject /]的HTTP请求的映射

此外,日志显示正在部署:

Jan 3, 2012 2:24:07 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor AnotherMvcProject.xml from /Users/myname/springsource/vfabric-tc-server-developer-2.6.1.RELEASE/spring-insight-instance/conf/Catalina/localhost

这是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>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

这是ServletContext:

<?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">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="xyz.mvc.project" />

</beans:beans>

这是控制器代码:

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! the client locale is "+ locale.toString());

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate );

        return "home";
    }

}

代码与BareMvc相同,但包名称除外。 xyz.sample.baremvc与xyz.mvc.project。

我不清楚如何指定从应用程序名称到servlet引擎的映射。看起来它可能是pom.xml,项目,但这些也是一致的:

groupId=xyz.sample
m2e.projectName=BareMvc

groupId=xyz.mvc
m2e.projectName=AnotherMvcProject

我很难过。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

供将来参考 - 只需清理项目即可解决问题。