RequestMapping not working - java

I build a Spring project with netbean and got some problem.
I simply put a form and a submit input which supposed to be processed by StudentController but I got 404 when the button is clicked.
Am I doing something wrong?
index.jsp
<body>
<form method="GET" action="student">
<input type="submit" value="submit">
</form>
</body>
StudentController.java
#Controller
public class StudentController {
#RequestMapping(value = "/student",method = RequestMethod.GET)
public ModelAndView student(){
return new ModelAndView("student", "command", new Student());
}
}
web.xml(Generate by NetBean)
<?xml version="1.0" encoding="UTF-8"?>
<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>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
applicationContext.xml(Generate by NetBean)
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
</beans>
dispatcher-servlet.xml(Generate by NetBean)
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
Sorry for posting all the codes here, because I have really no idea where will the problem be. Please help me. Thanks in advance!

Your Spring servlet is mapped to *.htm:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
But your form sends a request to /student. *.htm doesn't match /student, so obviously, you get a 404.

I think it is beacause the dispatcher servlet only listens on .htm requests, but you request localhost:8080/Student/student.
Try
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

Advised by #JB Nizet, I removed
ControllerClassNameHandlerMapping
urlMapping
indexController
from the dispatcher-servlet.xml file, and simply replaced it by <mvc:annotation-driven />.
What's more, <context:component-scan base-package="com.mycompany.myapp.controllers"></context:component-scan> was add to the dispatcher-servlet.xml file.
Also, you need to specify xsi:schemaLocation, so add
"http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
to the xsi:schemaLocation attribute.
And after all of these, I edited index.jsp to : (make student as student.htm)
<form method="GET" action="student.htm">
<input type="submit" value="submit">
</form>
And simply wrote those in my StudentController.java:
#RequestMapping(value="/index.htm", method=RequestMethod.GET)
public String index(){
return "index";
}
#RequestMapping(value = "/student.htm",method = RequestMethod.GET)
public ModelAndView student(){
return new ModelAndView("student", "command", new Student());
}
After those, the problem was fixed.
Maybe I should consider useing Eclipse instead of NetBean to build Spring.

Related

Spring MVC: Tomcat 404 The requested resource is not available

i am new in Spring framework and
working on HelloSpring project and I am still getting the HTTP Status 404 error.
I am pretty desperate right now. May anyone tell me, what is the problem, please?
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.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>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
dispatcher-servlet.xml
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> --><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<context:component-scan base-package="controllers" />
<mvc:annotation-driven />
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
IndexController.java
#Controller
public class IndexController{
#RequestMapping("welcome")
public String index(ModelMap map, HttpServletRequest r){
System.out.println("Kontrola do konzole");
map.put("msg", "This is index page");
return "index";
}
}
redirect.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%><% response.sendRedirect("welcome"); %>
Tomcat Log
05-Feb-2017 14:14:00.583 WARNING [http-nio-8084-exec-495] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/MavenHello/welcome] in DispatcherServlet with name 'dispatcher'
When i Run the application, I always get 404 Error on URL http://localhost:8084/MavenHello/welcome
So let's start it all over again :
1# Add this snippet in your web.xml (get rid of redirect.jsp we don't need it anymore)
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
2# In dispatcher-servlet.xml put this snippet (get raid of all others configs) :
Be sure to change xx.yy.zz.controllers with your real package ( where your are putting controllers.)
<context:component-scan base-package="xx.yy.zz.controllers" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
3# Make sure your IndexController look like this:
package xx.yy.zz.controllers;
//imports here...
#Controller
public class IndexController {
#RequestMapping(value={"/", "/index", "/welcome"}, method = RequestMethod.GET)
public String index(Map<String, Object> model) {
System.out.println("Kontrola do konzole");
map.put("msg", "This is index page");
//it will be redirected to /WEB-INF/jsp/index.jsp (make sure that have this page)
return "index";
}
}
4# And finally add a /WEB-INF/jsp/index.jsp
<html>
<body>
<c:if test="${not empty msg}">
Hello ${msg}
</c:if>
</body>
</html>
That's it, now you can test your app using one of the following urls:
http://localhost:8084/MavenHello/
http://localhost:8084/MavenHello/index
http://localhost:8084/MavenHello/welcome
Let me know if sth goes wrong, good luck.

Spring call controller action from jsp by <a> tag

I'm a newbie Spring MVC, I can't call controller from jsp by tag <a>. I try a lots but don't know why. When I click the link, the server announced error 404. Please help me !
My controller
#Controller
#RequestMapping(value = "/form",method=RequestMethod.GET)
public class formController {
#RequestMapping(value="/registry", method=RequestMethod.GET)
public String registry (ModelMap mm){
mm.addAttribute("user",new user());
return "registry";
}
#RequestMapping(value="/regProccess", method = RequestMethod.POST)
public String regProccess(#ModelAttribute(value="user") user user,ModelMap mm){
mm.put("user",user);
return "welcome";
}
}
My jsp
<html>
<body>
Registry
</body>
</html>
web.xml
<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>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
dispatcher-servlet
<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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>

PathVariable does not work at all

For some reason, my #PathVariable annotation is not working at all, after doing some Google search I've not been able to find anyone else with the same issue, this is the code:
#Controller
#RequestMapping("/bot")
public class BotController {
#RequestMapping(value = "/test", method = RequestMethod.GET)
public void test() {
System.out.println("test");
Store.INSTANCE.getChatBot().postMessage("test");
}
#RequestMapping(value = "/say/{text}", method = RequestMethod.GET)
public void say(final #PathVariable("text") String text) {
System.out.println("say: " + text);
Store.INSTANCE.getChatBot().postMessage(text);
}
}
This works: http://localhost:8080/GithubHookSEChatService/bot/test
This does not work: http://localhost:8080/GithubHookSEChatService/bot/say/realtest
Apart from that the System.out.println("say: " + text) does not happen, the only other clue I have is:
24-Aug-2014 17:25:21.611 WARNING [http-apr-8080-exec-24]
org.springframework.web.servlet.PageNotFound.noHandlerFound
No mapping found for HTTP request with URI
[/GithubHookSEChatService/bot/say/realtest] in DispatcherServlet with name 'dispatcher'
I'm running out of clues, does anyone know what is happening? Why doesn't the latter work?
My relevant web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
and
dispatcher-servlet.xml:
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" xmlns:context="http://www.springframework.org/schema/context">
<!-- auto scan -->
<context:component-scan base-package="com.skiwi.githubhooksechatservice" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:githubhooksechatservice-environment.properties</value>
</property>
</bean>
<!-- properties -->
<bean id="configuration" class="com.skiwi.githubhooksechatservice.mvc.configuration.Configuration">
<property name="rootUrl" value="${env.rootUrl}"/>
<property name="chatUrl" value="${env.chatUrl}"/>
<property name="botEmail" value="${env.botEmail}"/>
<property name="botPassword" value="${env.botPassword}"/>
<property name="roomId" value="${env.roomId}"/>
</bean>
<!-- startup bean -->
<bean name="startup" init-method="start" class="com.skiwi.githubhooksechatservice.mvc.beans.StartupBean" lazy-init="false" />
</beans>
This issue appears to not be related to #PathVariable only, it actually also fails on:
#RequestMapping(value = "/test/test", method = RequestMethod.GET)
#ResponseBody
public void test() {
System.out.println("test");
Store.INSTANCE.getChatBot().postMessage("test");
}
This happens because there are multiple levels of subpaths, I found the solution of my issue in In Spring MVC, how can I map nested URLs such as /settings/, /settings/users/, and /settings/users/delete?
The only thing you need to do, is to include the following in your dispatcher-servlet.xml:
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
And then everything works as expected!
I know this is old question. But the answer will be helpful for others.
(1) Add below line to dispatcher-servlet.xml assuming dispatcher is your DispatcherServlet name in web.xml
<mvc:annotation-driven/>

Spring 3.0 newbie start project not display value

I made simple helloworld in Netbeans 7.3. But I have problem with load jsp with value from controller. I partially inspire instructions from http://www.tutorialspoint.com/spring/spring_tutorial.pdf (page 141.). But when I want load jsp with value which is set in controller, value is not displayed. In controller are capture GET reguests. I entered this url: http://localhost:8080/HelloWorld3/hello.htm. I get jsp, but value not displayed. When I enter url which is write in above tutorial:
http://localhost:8080/HelloWeb/hello , I get 404 error, this page is not found.
What url I must enter or what I doing worse? I guess that error is in some config file. Thx
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="cz.ryska.controllers"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
dipatcher-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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
<prop key="hello.htm">helloController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
<bean name="helloController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="hello" />
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.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>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
controller:
#Controller
#RequestMapping("/hello")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
hello.jsp:
<%#page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<h1>Hello!!!!!!</h1>
<h2>${message}</h2>
</body>
</html>
Try this
#Controller
#RequestMapping("/hello")
public class MyController {
#RequestMapping(method = RequestMethod.GET)
public String sayHello(ModelMap model) {
model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";
}
}
hello.jsp
<html>
<body>
<h1>Message : ${message}</h1>
</body>
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-servlet.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>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
dispatcher-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"
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">
<context:component-scan base-package="com.gemini.spring.mvc"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Is there some particular reason you coded your helloWorld application the way you did?
For a simple helloWorld application it seems needlessly complex.
For one thing unless you had a some specific reason for doing so, you don't really need to define two separate controllers.
I also worked through the tutorialspoint example you linked and mine worked perfectly, but it was a clone of the original example.
If you want to extend or play around with the example I would suggest you first clone the example and get it working... then go forward from there, one step at a time.
While I don't have my solution in front of me, the answer given by Georgy Gobozov looks correct.

No mapping found in DispatcherServlet without using extension

I am receiving the message "The requested resource () is not available." when going to a url that I have mapped using DispatcherServlet. It previously worked when I had the mapping setup to use a .do extension, but when I map without an extension it doesn't work.
Controller:
#Controller
public class WodServiceController {
#Autowired
private WodService wodService;
#RequestMapping("/json/wod")
#ResponseBody
public Word getWord(HttpServletRequest request, HttpServletResponse response) {
return wodService.getWordOfTheDay();
}
}
dispatcher-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:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="foobar.controller" />
<!-- View Resolvers -->
<bean id="defaultViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
id="WebApp_ID" version="2.5">
<display-name>Services</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:commonBeans.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>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/json/*</url-pattern>
</servlet-mapping>
</web-app>
If I have the mapping set to *.do and the RequestMapping set to wod.do it works fine.
EDIT:
In the logs, when I go to that URL it shows the message
No mapping found for HTTP request with URI [/foobar/json/wod] in DispatcherServlet with name 'dispatcher'
EDIT 2: I added the alwaysUseFullPath to the handler:
<bean id="annotationHandlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
</bean>
This was due to the way my apache redirect was setup.

Categories