首先,我无法获取索引页面,但我使用404 error for tomcat 6 for spring application
解决了该问题获得索引页面后,当我点击链接时,我无法将请求映射到我的控制器。
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.
答案 0 :(得分:1)
最重要的是,你错过了
<mvc:annotation-driven />
在您的servlet上下文中,它发现了@Controller
带注释的bean及其@RequestMapping
带注释的方法等。
没有它,您的控制器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>