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

ghg1uchk  于 2023-04-10  发布在  Spring
关注(0)|答案(1)|浏览(282)

我正在使用带注解控制器的SpringMVC 3。我成功地将我的URL(“/HelloWorld)Map到一个控制器并定义了它的GET处理方法。
错误是在键入(App)/HelloWorld URL时,我的Web服务器(GlassFish)给出以下错误:

请求的资源不可用。

但是在GlassFish日志中,我看到URL被挂载。
Map"{[/HelloWorld],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}"ontopublic 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”?

f5emj3cl

f5emj3cl1#

问题解决了。我不得不调用URL“/HelloWorld.htm”以使Map工作。
(The Dispatcher-Servlet在web.xml中的Map是“*.htm”。)谢谢

相关问题