2012-05-04 34 views
0

我已經創建了一個簡單的Maven項目,它具有一個簡單的控制器類,它只顯示服務器時間,但它永遠不會通過控制器,會很樂意提供任何幫助。HTTP請求將不會通過控制器

我的控制器類:

@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 

彈簧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> 

索引.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} 

請任何幫助嗎?

+0

我不明白爲什麼它應該。我會用'getServerTime()'方法創建一些實用程序類,並將其導入到JSP中並按照時間進行導入。 –

+0

你能發佈你得到的錯誤/堆棧跟蹤嗎? – Raghuram

+0

沒有任何錯誤輸出,它顯示了上面提到的索引頁面,但沒有顯示時間,甚至沒有顯示System.out.println(「hello」); ,無法打印。它從不會拋出控制器類。 – user1067665

回答

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。

只是爲了調試,我會嘗試的另一件事是將這些條目明確地放入您的應用程序web.xml文件中。

更新:@ user1067665根據您的評論嘗試多一點之後,它肯定聽起來不像Tomcat問題,它更像是應用程序配置問題。我認爲可以修復的是將你的AnnotationHandlerAdapter和DefaultAnnotationHandlerMapping的定義替換爲<mvc:annotation-driven/>,你可以試試看看是否可以運行

+0

是的,我已經檢查過它,這些行位於config文件夾中的web.xml中。我相信問題是Tomcat,但不知道在哪裏和什麼。我在web.xml中也嘗試了這些行,但得到了相同的結果。 – user1067665

+0

好的,我可以請求你嘗試一件事 - 你可以使用maven中的tomcat插件來啓動你的應用程序,看看你的jsp是否還沒有被解釋 - 這是你如何添加一個tomcat插件:\t org.codehaus.mojo tomcat-maven-plugin 1。0並使用mvn tomcat啓動它:運行 –

+0

是tomcat啓動並運行。 – user1067665

1

你也已經映射了你的請求......路徑將是/ index/index

從方法級別註釋中除去「value」參數,可以保留「method」參數。

+0

使用我們的無值參數,我會得到相同的輸出。所以它不是關於值參數。我已經嘗試了一切,沒有任何結果。 – user1067665