2014-02-14 124 views
0

首先,我無法獲得索引頁,但我決心用404 error for tomcat 6 for spring application無法在Spring MVC映射請求控制器3

我得到索引頁之後的問題,當我點擊鏈接,我無法映射請求我的控制器。

studentspringmvc.xml

<Context path="/studentspringmvc" 
    docBase="/home/shoaib/Documents/myprograms/studentspringmvc/src/main/webapp" 
    reloadable="true" 
    debug="9" /> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
    <web-app version="2.5" 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_2_5.xsd"> 

<display-name>Spring MVC Application</display-name> 

<context-param> 
    <param-name>contextConfigLocations</param-name> 
    <param-value>classpath*:applicationContext.xml</param-value> 
</context-param> 


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

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

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 

StudentDetailsController.java

package com.semanticbits.studentspringapp.controller; 

import org.apache.log4j.Logger; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 


@Controller 
@RequestMapping("/studentDetails") 
public class StudentDetailsController { 

    private static final Logger logger = Logger.getLogger(StudentDetailsController.class); 

    public StudentDetailsController() { 
     System.out.println("In Student Constructor"); 
    } 

    @RequestMapping(method=RequestMethod.GET) 
    public String showForm(ModelMap map){ 
    logger.info("In service method"); 
    System.out.println("In Student Details"); 
    return "studentdetails"; 
    } 
} 

MVC-調度-servlet.xml中

<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.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

<context:component-scan base-package="com.semanticbits.studentspringapp.*"/> 
<mvc:annotation-driven/> 
<mvc:default-servlet-handler/> 


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/pages/"/> 
    <property name="suffix" value=".jsp"/> 
</bean> 

的index.jsp

<!DOCTYPE html> 
<html> 
<head> 
<title></title> 
</head> 
<body> 
    <a href="/studentDetails">Click here</a> 
</body> 
</html> 

當我點擊此鏈接,它應該得到地圖StudentDetailsController showForm方法顯示另一個studentdetails.jsp但我越來越404。

回答

1

最重要的是,你在你的servlet上下文,它發現@Controller註解豆類及其@RequestMapping註解的方法,尤其缺少

<mvc:annotation-driven /> 

如果沒有它,您的控制器bean將停留在上下文中,而沒有太多工作。 DispatcherServlet不註冊它們。

然後,當你在

<a href="/studentDetails">Click here</a> 

指定領先/瀏覽器使得相對於你的主機地址的請求。所以,如果你的主機IP是像

127.0.0.1 

那麼請求將要

127.0.0.1/studentDetails 

製成,但你似乎有一個上下文路徑

<Context path="/studentspringmvc" 

所以,你需要將其更改爲

<a href="/studentspringmvc/studentDetails">Click here</a> 

或更好但是,make it dynamic

<a href="${pageContext.request.contextPath}/studentDetails">Click here</a> 
+0

不起作用。請參閱我的studentspringmvc.xml和web.xml以及mvc-dispatcher-servlet。xml –

+0

@ShoaibChikate查看我的更新。 –

+0

並且我應該在控制器中添加相同的路徑@RequestMapping –