0

注釈付きコントローラーで SpringMVC 3 を使用しています。URL ("/HelloWorld) をコントローラーに正常にマップし、その GET 処理メソッドを定義しました。

エラーは、(App)/HelloWorldURL を入力すると、Web サーバー (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 に答える