带有注释控制器的 SpringMVC:URL "Resource Not Available"



我正在使用带有注释控制器的SpringMVC 3。我成功地将我的 URL("/HelloWorld")映射到控制器并定义了它的 GET 处理方法。

错误是,在键入(App)/HelloWorld URL时,我的网络服务器(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) 调度程序-服务.xml。请注意 MVC 注释驱动方法。不使用索引控制器。

<?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)你好世界页面.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"吗?

问题解决了。我必须调用 URL"/HelloWorld.htm"才能使映射正常工作。

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

最新更新