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/>
Related
I am starting with Spring Framework and want to do a HelloWorld with annotatios, I've made it work creating a controller and a view, basic hello work I guess; however, I want to use annotatios since I can not use SimpleFormController any more (deprecated).
The error I am getting is Estado HTTP 404 - /av/index.jsp
I am using Netbeans and I am basing the example on the basic template It provides. I have the following files, I am pretty sure it is a missconfiguration but I can't find anything that would help me so far. Thanks in advance.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</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>
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"
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">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="annotationHandlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1"/>
<property name="alwaysUseFullPath" value="true"/>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<context:component-scan base-package="controller"/>
</beans>
indexController.java
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;
#Controller
public class IndexController {
#RequestMapping(value="/index.do", method= RequestMethod.GET)
public ModelAndView inicio (){
ModelAndView mv = new ModelAndView("index");
mv.addObject("usuario", "jaxkodex");
return mv;
}
}
You are missing
<mvc:annotation-driven />
in your config dispatcher-servlet.xml
See here for more info
You mapped the path /index.do in your controller, so you have to access it with the following url: http://localhost/av/index.do
<mvc:annotation-driven/> will solve out your problem.
Please try this
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
add an entry in dispatcher-servlet.xml
in your disapatcher-servlet.xml, add
<mvc:annotation-driven/> to enable annotation driven handler mapping.
Try to go through one of my tutorials:
Creation of a simple Controller with java-based config
I hope I will get success with it
This is the web.xml, it should be correct because it is generated by Intellij.
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="3.1">
<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>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
</web-app>
This is the controller
#Controller
public class IndexController {
#RequestMapping("/index")
public String index(ModelMap model) {
System.out.print("index");
return "index";
}
}
when I create new project in Intellij, I checked these options.
Java EE > Web Application
> Spring MVC
> Hibernate
I cannot access the page correctly
if I use "localhost:8080/test"
I can see the page, but the controller is not executed, because this URL will access "localhost:8080/test/index.jsp"
if I use "localhost:8080/test/index", result is 404
if I use "localhost:8080/test/index", result is 404, and server log will show this error
WARNING: No mapping found for HTTP request with URI [/test/index.form] in DispatcherServlet with name 'dispatcher'
I changed ".form" to "/", but I still got same error.
What is still missing in my project?
Update, these are applicationContext.xml and dispatcher-servlet.xml, these files are also generated by Intellij
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
Your IndexController is not being found by Spring.
You added a #Controller but you didn't tell Spring load them.
Change your dispatcher-servlet.xml and add this.
The important part here is the context:component-scan which tells Spring to look for annotated classes inside the package.
Here is the reference.
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-controller
<?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"
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="<your controller package here>" />
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
</bean>
</beans>
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.
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.
I am starting with Spring Framework and want to do a HelloWorld with annotatios, I've made it work creating a controller and a view, basic hello work I guess; however, I want to use annotatios since I can not use SimpleFormController any more (deprecated).
The error I am getting is Estado HTTP 404 - /av/index.jsp
I am using Netbeans and I am basing the example on the basic template It provides. I have the following files, I am pretty sure it is a missconfiguration but I can't find anything that would help me so far. Thanks in advance.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</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>
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"
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">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="annotationHandlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1"/>
<property name="alwaysUseFullPath" value="true"/>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<context:component-scan base-package="controller"/>
</beans>
indexController.java
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;
#Controller
public class IndexController {
#RequestMapping(value="/index.do", method= RequestMethod.GET)
public ModelAndView inicio (){
ModelAndView mv = new ModelAndView("index");
mv.addObject("usuario", "jaxkodex");
return mv;
}
}
You are missing
<mvc:annotation-driven />
in your config dispatcher-servlet.xml
See here for more info
You mapped the path /index.do in your controller, so you have to access it with the following url: http://localhost/av/index.do
<mvc:annotation-driven/> will solve out your problem.
Please try this
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
add an entry in dispatcher-servlet.xml
in your disapatcher-servlet.xml, add
<mvc:annotation-driven/> to enable annotation driven handler mapping.
Try to go through one of my tutorials:
Creation of a simple Controller with java-based config
I hope I will get success with it