Влад
Влад

Reputation: 3

Controller does't work in Spring web error 404

I do this lesson but my controller don't work error 404, before it worked well. jsp files work well. does't work only controller. The console has no errors. What happened?

Controller:

@Controller
public class HelloController {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String printHello(ModelMap model) {
        model.addAttribute("message", "Hello Spring MVC Framework!");
        return "hello";
    }
}

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" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 id="WebApp_ID" version="3.0">
  <display-name>Servlets</display-name>
   <servlet>
      <servlet-name>HelloWeb</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>HelloWeb</servlet-name>
      <url-pattern>*.jsp</url-pattern>
   </servlet-mapping>
</web-app>

HelloWeb-servlet.xml:

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:context = "http://www.springframework.org/schema/context"
   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-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.tutorialspoint" />

   <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name = "prefix" value = "/WEB-INF/jsp/" />
      <property name = "suffix" value = ".jsp" />
   </bean>

</beans>

hello.jsp

<html>
   <head>
      <title>Hello Spring MVC</title>
   </head>

   <body>
      <h2>${message}</h2>
   </body>
</html>

Folder structure

Upvotes: 0

Views: 80

Answers (1)

M. Rizzo
M. Rizzo

Reputation: 2251

A couple of things I'm noticing.
1) An <?xml tag declaration within a web.xml file is actually not allowed. Your JSP editor within Eclipse should have indicated an error. Please remove <?xml version="1.0" encoding="UTF-8"?>

2) I think you are running into issues between default servlet container handlers and mappings for the Dispatcher servlet. Can you change the servlet mapping in your web.xml file from

<servlet-mapping>
  <servlet-name>HelloWeb</servlet-name>
  <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

to

<servlet-mapping>
  <servlet-name>HelloWeb</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

which is the default handling for Spring dispatcher.

I would perform those changes, perform a rebuild of your project, redeploy to your container and try hitting your controller mapping http://<yourhost_root>/HelloWeb/hello/

Upvotes: 1

Related Questions