0

サーバー時間のみを表示する単純なコントローラークラスを使用して単純な Maven プロジェクトを作成しましたが、コントローラーを通過することはありません。

私のコントローラークラス:

@Controller
@RequestMapping(value = "/")
public class Test
{
private static final Logger logger = LoggerFactory.getLogger(StudentsController.class);


@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.info("Welcome home! the client locale is "+ locale.toString());

    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate );
    System.out.println(formattedDate);

    return "index";
}

} 

web.xml

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

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

 <servlet-mapping>
 <servlet-name>test</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
 </web-a

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:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

      <!-- not strictly necessary for this example, but still useful, see       
       http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference 
       /html/mvc.html#mvc-ann-controller for more information -->
       <context:component-scan base-package="test" />
       <mvc:default-servlet-handler/>


       <!-- the mvc resources tag does the magic -->
      <mvc:resources mapping="/resources/**" location="/resources/" />

        <!-- also add the following beans to get rid of some exceptions -->
       <bean        
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
       <bean
         class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        </bean>

     <!-- JSTL resolver -->
      <bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="viewClass"
     value="org.springframework.web.servlet.view.JstlView" />
       <property name="prefix" value="/WEB-INF/jsp/" />
       <property name="suffix" value=".jsp" />
      </bean>

index.jsp

  <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  <html>
<head>
    <title>Spring RESTful Example</title>
</head>
<body>
    <p>Your WebApplication is up and running....</p>

    <div>
    Server Time is:

<div>
${serverTime} 

</div>  
    </div>
</body>
  </html>

Tomcatサーバーで実行すると、出力は次のようになります。

Your WebApplication is up and running....
 Server Time is:
 ${serverTime}

助けてください。

4

2 に答える 2

1

また、リクエストをダブルマッピングしました...パスは/ index/indexになります

メソッドレベルのアノテーションから「value」パラメータを削除します。「method」パラメータはそのままにしておくことができます。

于 2012-05-04T10:12:50.850 に答える
1

問題は Tomcat にあるようです。Tomcat.home\conf フォルダーに web.xml ファイルがあり、次の行に沿ったエントリがあるかどうかを確認できますか?

<servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    ....


<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>*.jspx</url-pattern>
</servlet-mapping>

これは、jsp を解釈するものです。

デバッグするために、もう 1 つ試みたいのは、これらのエントリをアプリケーションの web.xml ファイルにも明示的に配置することです。

更新: @ user1067665 コメントに基づいてもう少し試してみたところ、Tomcat の問題ではなく、アプリケーションの構成の問題です。これを修正するには、AnnotationHandlerAdapter と DefaultAnnotationHandlerMapping の定義を に置き換えることだと思います。<mvc:annotation-driven/>これを試して、うまくいくかどうかを確認してください。

于 2012-05-04T09:21:55.890 に答える