0

シンプルなSpring MVCアプリを作成しています。以下のような URL パターンを受け入れるように春の DispatcherServlet を構成するにはどうすればよいですか。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

現在、アプリの起動時に警告を下回っています。

WARNING: No mapping found for HTTP request with URI [/SpringMVC/] in DispatcherServlet with name 'spring'

spring-servlet.xml (デフォルトのサーブレット ハンドラーも追加しました)

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

    <context:annotation-config />
    <context:component-scan
        base-package="com.springapp" />

    <mvc:default-servlet-handler/>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:resources/messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
4

4 に答える 4

2

まず、web.xml に DispatcherServlet マッピングを含める必要があります (dispatcher-servlet.xml ファイルの場所を指定するか、デフォルトを使用できます)。

    <servlet>
            <servlet-name>dispatcher-servlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                    <param-name>contextConfigLocation</param-name>
                    <param-value>/WEB-INF/classes/spring-servlet.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
            <servlet-name>dispatcher-servlet</servlet-name>
            <url-pattern>/</url-pattern>
    </servlet-mapping>

これを spring-servlet.xml ファイルに追加します。

<mvc:annotation-driven/>

そして主要なステップとして、パス /SpringMVC にマップされるコントローラーを作成する必要があります。

package com.springapp.controllers;

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


@Controller
@RequestMapping("/SpringMVC")
public class MyController{

     @RequestMapping(method= RequestMethod.GET)
     public ModelAndView springMvcTest(ModelMap modelMap){
         return new ModelAndView("test");
     }
}
于 2015-02-15T21:48:46.223 に答える
1

次のいずれかを使用してみてください。

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/SpringMVC/*</url-pattern>
</servlet-mapping>

または:

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.form</url-pattern>
</servlet-mapping>
于 2012-07-01T13:04:58.543 に答える
0

次のように、コントローラーでこの URL をマップする必要があります。

 @RequestMapping("/SpringMVC")
   public ModelAndView index(HttpServletRequest req,HttpServletResponse){

     // ur business logic

   return new ModelAndView("index");  
  }
于 2012-07-02T09:34:46.717 に答える