Reputation: 8975
Firstly, I was unable to get the index page but I resolved that issue using 404 error for tomcat 6 for spring application
After I get index page and when I click on link I am unable to map the request to my Controller.
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-dispatcher-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>
When I click on this link it should get map to StudentDetailsController showForm method displaying another studentdetails.jsp but I am getting 404.
Upvotes: 0
Views: 3204
Reputation: 279970
Most importantly, you're missing
<mvc:annotation-driven />
in your servlet context, which discovers @Controller
annotated beans and their @RequestMapping
annotated methods, among other things.
Without it, your controller beans stay in the context, not doing much. The DispatcherServlet
doesn't register them.
Then, when you specify a leading /
in
<a href="/studentDetails">Click here</a>
the browser makes the request relative to your host address. So if your host IP is like
127.0.0.1
then the request will be made to
127.0.0.1/studentDetails
But you seem to have a context path
<Context path="/studentspringmvc"
So you need to change it to
<a href="/studentspringmvc/studentDetails">Click here</a>
or better yet, make it dynamic
<a href="${pageContext.request.contextPath}/studentDetails">Click here</a>
Upvotes: 2