Spring MVC @RequestMapping for class level and method level not working

Viewed 1689

I try put 2 request mapping since i want to point to home/home since later i will be having another controller with same name which is about/home. But i not sure why it was not working. If have only home it is working but home/home or about/home not working. The class level not working.

This the controller

package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.constant.server.HelperConstant;

@Controller
@RequestMapping("/home")
public class HomeController 
{
    @RequestMapping("/home")
    public String doDisplayPage()
    {
        return HelperConstant.VIEW_HOME;
    }

}

This the jsp

<html>
<body>
    <form action="home/home">
        <input type="text" name="t1"><br>
        <input type="text" name = "t2"><br>
        <input type ="submit">
    </form>
</body>
</html>

I am hitting HTTP Status 404 -

Edited

Attached the config files

package com.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // TODO Auto-generated method stub
        return new Class[] {SpringConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        return new String[] {"/"};
    }

}

This another config file

package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan({"com.controller"})
public class SpringConfig 
{
    @Bean
    public InternalResourceViewResolver viewResolver()
    {
        InternalResourceViewResolver vr = new InternalResourceViewResolver();

        //vr.setPrefix("/WEB-INF/");
        vr.setSuffix(".jsp");

        return vr;
    }

}

Attached the picture of the folder structure enter image description here

there is an additional findings as well whereby i trying to look for the jsp at home/home.jsp. can i know why such behaviour? by right should just look at home.jsp. If remvoe class level anotation, then it working fine or i create a folder inside webapp home it working fine but after click submit again it looks for home/home/home.jsp

I have added a method="POST"

now problem is after restart tomact

enter image description here

after click submit like this

enter image description here

if click submit again show eror 404

enter image description here

this my web.xml but i removed it already since i already use java or configurtion

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

   <welcome-file-list>
        <welcome-file>home.html</welcome-file>
        <welcome-file>home.htm</welcome-file>
        <welcome-file>home.jsp</welcome-file>
    </welcome-file-list>

</web-app>

7 Answers

The problem is your action path <form action="home/home"> This is a relative path meaning it will do the following.

http://example.com/web_main/home - form action - > 
http://example.com/web_main/home/home (200) - form action -> 
http://example.com/web_main/home/home/home (404)

Others are suggesting /home/home but that isn't taking to account your application context path. The following would be the result.

http://example.com/web_main/home - form action - > http://example.com/home/home

Notice that the web_main is gone.

To fix your issue you need to leverage JSTL, add this to the JSP.

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

<c:url value="/home/home" var="homeUrl" />`
<form action="${homeUrl}">

Add the import to the top of the JSP.

There is one issue in the url. Have you observed the actual url? It is displaying /home/home/home after the context but url points to /home/home. Solution might be removing one /home from the action url in the form tag

<html>
<body>
    <form **action="home"**>
        <input type="text" name="t1"><br>
        <input type="text" name = "t2"><br>
        <input type ="submit">
    </form>
</body>
</html>

or

<html>
<body>
    <form action="/home/home">
        <input type="text" name="t1"><br>
        <input type="text" name = "t2"><br>
        <input type ="submit">
    </form>
</body>
</html>

What does your Spring app class look like? If you do not have one you should create one. Instead of having the @ComponentScan below the @Configuration tag, do something like below and it should be picked up easier, also maybe change your root tag to just / for easier for reading:

@SpringBootApplication
@ComponentScan(basePackages = {"com.*"})
class MyClass {
   public static void main(String[] args) {
      SpringApplication.run(MyClass.class)
   }
}

@Controller
@RequestMapping("/")
public class HomeController 
{
    @RequestMapping("/home")
    public String doDisplayPage()
    {
        return HelperConstant.VIEW_HOME;
    }

}

When using RequestMapping default request mapping method value is GET method. Html form submit uses POST method by default. So you need to add post method to this url. You can give multiple method value for same request mapping.

This is works

@Controller
@RequestMapping("/home")
public class HomeController {

    @RequestMapping(value = "/home")
    public String doDisplayPage() {
        return "../home";
    }
}
@Controller
@RequestMapping("/about")
public class AboutController {

    @RequestMapping(value = "/home")
    public String doDisplayPage() {
        return "../home";
    }
}
<html>
<body>
    <form action="/home/home">
        <input type="text" name="t1"><br>
        <input type="text" name = "t2"><br>
        <input type ="submit">
    </form>
</body>
</html>

enter image description here

Also you don't need web.xml since you have describe deployment descriptor in your WebConfig and SpringConfig

in 404 tab there are 3 home not 2 as you want in URL so its showing 404 . Instead of relative path home/home in submit form give absolute /home/home

If you are only trying to make your current code work, then a simple solution is to append Context Root to your form action as shown below.

<html>
<head>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
</head>
<body>
    <form action="${ctx}/home/home">
        <input type="text" name="t1"><br> <input type="text"
            name="t2"><br> <input type="submit">
    </form>
</body>
</html>
Related