Reputation: 460
I'm studying spring mvc and want to test some very simple annotation-based controllers,It works fine on home page but when I enter /home/user it gives me HTTP Status 404 – Not Found error and I haven't found anything to fix it.
web.xml
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--####################################-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<context:component-scan base-package="spring" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--<bean id="viewResolver"
</beans>
HomeController.java
package spring.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
//@RequestMapping(value = "/home")
public class HomeController {
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String showHomeMessage() {
return "home";
}
}
UserController.java
package spring.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/home/user")
public class UserController {
@RequestMapping(method = RequestMethod.GET)
public String welcomeMessage(){
return "user";
}
}
redirect.jsp just uses sendRedirect()
method and redirects to home.jsp.
home.jsp and user.jsp just prints a simple hello message
Where I'm doing wrong?
Regards
Upvotes: 1
Views: 121
Reputation: 2202
Have you tried nesting the RequestMapping
? Something like this...
@Controller
@RequestMapping(value = "/home")
public class HomeController {
@RequestMapping(method = RequestMethod.GET)
public String showHomeMessage() {
return "home";
}
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String welcomeMessage(){
return "user";
}
}
I'm not sure if you need to put a value on showHomeMessage
(i.e. value="/"
).
Upvotes: 1
Reputation: 168
After looking through this little example, I think your issue is tied to the fact that you are not specifying the @ResponseBody
using the @Controller
annotation. Whereas, if you use the @RequestController
annotation simply implying a String type would be sufficient because it combines the use of @Controller
and @ResponseBody
@Controller
@RequestMapping("/home/user")
public class UserController {
@RequestMapping(method = RequestMethod.GET)
public @ResponseBody String welcomeMessage(){
return "user";
}
}
The alternative to the same block would be...
@RestController
@RequestMapping("/home/user")
public class UserController {
@RequestMapping(method = RequestMethod.GET)
public String welcomeMessage(){
return "user";
}
}
Of course this little example doesnt demonstrate MediaType
and so forth, but hopefully you get the idea.
Upvotes: 0