I am trying to make a spring hello world program in eclipse. Here is the code
index.jsp
<a href="hello.html">click</a>
HelloWorldController.java
package com.javatpoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "HELLO SPRING MVC HOW R U";
return new ModelAndView("hellopage", "message", message);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
spring-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">
<context:component-scan base-package="com.javatpoint" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
hellopage.jsp
Message is: ${message}
When I go to http://localhost:8080/SpringMVCBasic/ , I gets the index.jsp page correctly open
but when I click on link on the page ie the url http://localhost:8080/SpringMVCBasic/hello.html , I gets the error
HTTP Status 404 -
When I use url like this http://localhost:8080/SpringMVCBasic/hello , I gets this
HTTP Status 404 - /SpringMVCBasic/hello
I have taken the code from here
You provided mapping for:
@RequestMapping("/hello")
but you have to do it for:
@RequestMapping("/hello.html")
I downloaded the eclipse project and run it on a jboss. Following jars needed to be included:
If i afterwards run the jboss with Java 8 all seems to work fine.
在spring-servlet.xml中启用Spring MVC @Controller编程模型,如下所示:
<mvc:annotation-driven />
In your index.jsp instead of writing
href="hello.html">
change to
href="./hello.html">
and
In HelloworldController.java
instead of writing
@RequestMapping("/hello")
change to:-
@RequestMapping(value="/hello.html")
Please add all these jar as external jar or add these jar to your lib folder
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.