带注释控制器的SpringMVC:URL“资源不可用”

时间:2013-09-07 15:45:35

标签: java spring controller annotations

我正在使用SpringMVC 3和Annotated Controllers。我成功地将我的URL(“/ HelloWorld”)映射到Controller并定义了它的GET处理方法。

错误是在输入(App)/HelloWorld网址时,我的网络服务器(GlassFish)会出现此错误:

请求的资源不可用。

但是在GlassFish日志中,我看到URL已经安装。

映射"{[/HelloWorld],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView controllers.HelloWorldController.processHelloWorld()

我的档案:

(1)HelloWorldController.java

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author      */
@Controller
@RequestMapping("/HelloWorld")
public class HelloWorldController {

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView processHelloWorld()
    {
    ModelAndView model = new ModelAndView("HelloWorldPage");
    model.addObject("msg", "Expanded string - hello world");

    return model;

    }
}

(2)Dispatcher-Servlet.xml。请注意MVC-Annotation-Driven方法。不使用indexController。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" 
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:component-scan base-package="controllers" />    
    <mvc:annotation-driven />  

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->    
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>


    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>

(3)HelloWorldPage.jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
    <h1>Spring MVC Hello World Annotation Example</h1>

    <h2>${msg}</h2>

</body>
</html>

为什么没有找到URL“/ HelloWorld”?感谢。

1 个答案:

答案 0 :(得分:0)

问题解决了。我必须调用URL“/HelloWorld.htm”才能使映射生效。

(Dispatcher-Servlet在web.xml中的映射是“* .htm”。)谢谢