数日前、私はこの Spring Hello World チュートリアルの勉強を始めました: http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/
このチュートリアルでは、Spring DispatcherServlet は、次の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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="net.viralpatel.spring3.controller" />
<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>
このファイルでは、context:component-scanタグを使用して、Spring が注釈を検索してファイルをスキャンする必要があることを示しています。たとえば、コントローラ クラスが@RequestMapping("/hello")注釈によってメソッドに注釈が付けられていることを検出した場合などです。このメソッドが「/hello」で終わる URL への HTTP リクエストを処理することがわかっています。これは簡単です...
私の疑問は、STS\Eclipse で自動的にビルドできる Spring MVC テンプレート プロジェクトに関連しています。
STS で新しい Spring MVC プロジェクトを作成すると、私のDispatcherServletは、前のサンプル ファイルと同様の構成を含むservlet-context.xmlという名前のファイルによって構成されます。
このファイルには、まだコンポーネント スキャン タグがあります。
<context:component-scan base-package="com.mycompany.maventestwebapp" />
しかし、別のタグ(同様のタスクがあるように見える)もあります。これは次のとおりです。
<annotation-driven />
この 2 つのタグの違いは何ですか?
もう 1 つの「奇妙な」点は、前の例 (アノテーション駆動タグを使用しない) が、Spring MVC テンプレート プロジェクトを使用して STS によって作成されたプロジェクトに非常に似ていることですが、構成からアノテーション駆動タグを削除すると、プロジェクトが実行されず、次のエラーが表示されます: HTTP ステータス 404 -
そして、私が持っているスタックトレースで:
WARN : org.springframework.web.servlet.PageNotFound - 名前が「appServlet」の DispatcherServlet で、URI [/maventestwebapp/] の HTTP リクエストのマッピングが見つかりません
しかし、なぜ?前の例は、アノテーション駆動型のタグがなくてもうまく機能し、このコントローラー クラスは非常に似ています。実際、「/」パスへの HTTP リクエストを処理するメソッドは 1 つだけです。
これは私のコントローラークラスのコードです:
package com.mycompany.maventestwebapp;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
誰かがこのことを理解するのを手伝ってくれますか?
どうもありがとうございました!