0

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

错误是在输入(App)/HelloWorldURL 时,我的网络服务器(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”?谢谢。

4

1 回答 1

0

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

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

于 2013-09-07T16:04:32.703 回答