I checked out nearly every relevant article on stackoverflow already, but I just cant fix my problem.
Here is the code: web.xml:
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>/</url-pattern>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
spring-servlet.xml:
<context:component-scan base-package="com.mycompany.elso" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<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/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
myController:
public class myController {
@RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
return new ModelAndView("hello", "message", message);
}
}
Web Pages/index.jsp:
<html>
<head>
<title>Spring 3.0 MVC Series</title>
</head>
<body>
<a href="hello.html">Say Hello</a>
</body>
</html>
Web Pages/WEB-INF/jsp/hello.jsp:
<html>
<head>
<title>Spring 3.0 MVC Series: Hello World - ViralPatel.net</title>
</head>
<body>
${message}
</body>
</html>
So when i launch the appication the index.jsp is loaded correctly but when i click on the href to navigate to hello.jsp i got a 404 error and the server log says:
No mapping found for HTTP request with URI [/Elso/hello.html] in DispatcherServlet with name 'spring'
I've checked out dozens of articles like that, but I just can't find the mistake, anybody has any idea what could it be?
Add
<mvc:default-servlet-handler/>
to spring-servlet.xml
You could try and add an @Controller
annotation on top of your myController Class and
try the following url /<webappname>/my/hello.html
.
This is because org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping
prepends /my to each RequestMapping in the myController class.
If you are using
<mvc:annotation-driven/>
make sure your spring-servlet.xml has correct
<context:component-scan base-package="com.....controller" /> tag.
Basically, you need to include all the packages where you have used the annotation in your java code.
Also, please ensure you do not have duplication of component-scan (for a discovery of beans). If your config XML already contains the element, then any of your Controller classes that are annotated with @ComponentScan(basePackages=... needs to be stripped of the said annotation.
I solved my issue with : Java Build Path -> JRE system library - > Edit -> Alternate JRE -> -> Finish
As it was configured to JDK folder so it was giving Exception
Make sure
<mvc:annotation-driven/>
<context:component-scan base-package="com.hireartists.web.controllers"/>
points to proper package that contains controllers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With