5

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>

17
  • what do you mean by "not working"? any error? Commented Oct 30, 2019 at 5:37
  • HTTP error 404. stating it not found the jsp which it looking for home/home.jsp. But why need look into the folder home and search for home.jsp Commented Oct 30, 2019 at 5:51
  • why not remove class level annotation and also change form action="home"? Commented Oct 30, 2019 at 6:11
  • @SoftwareEngineer well it's weird. have you tried /home/home for action attribute? Commented Oct 30, 2019 at 6:43
  • Default request mapping method is 'GET' method. Form submit uses 'POST' method by default. You can set method to POST Commented Oct 30, 2019 at 11:47

7 Answers 7

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

hi, it show s HTTP Status 404 - when click submit for first time. localhost:8080/web_main/$%7BhomeUrl%7D?t1=2&t2=2
i already add the dependency in maven <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
i added inside head <head> <%@ taglib prefix = "c" uri = "java.sun.com/jsp/jstl/core" %> <c:url value="/about/home" var="homeUrl" /> </head> then it was working. Thanks ya
@Software Engineer <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> is an import that you add to the top of the jsp file.
if you want, upload to github and i can make a comment directly
1

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>

1 Comment

i followed urs for the head section and @Dot Batch solution. Thanks ya
0

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;
    }

}

1 Comment

hi mine is spring mvc. Need do like this?
0

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.

5 Comments

<form action="home/home"> i tried, method level @RequestMapping(value = "home", method = RequestMethod.POST), class level @RequestMapping(value = "home"). but still not working got HTTP Status 405 - Request method 'GET' not supported; message :Request method 'GET' not supported; description: The specified HTTP method is not allowed for the requested resource.
You need to add post method on method level. And remove it from class level
Do you have any root url path ? If you have it you need to add it in form
i tried adding post method previously as well but was not workign
what is as root url?
0

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

2 Comments

yup this correct but why showing 3 home? by right should only be 1 home since it in home.jsp in webapp
it is due to redirection from application that URL is changed to home/home not to jsp which it is redirecting
0

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

6 Comments

hi still not working localhost:8080/web_main/home?t1=&t2= message description The requested resource is not available.
are you using localhost:8080/web_main/home to open the page for the first time before click submit? where is "web_main" came from? I did not see in your configuration
in the answer I wrote I use localhost:8080/home/home to open the page, then when submit is clicked, it will return to the same method and load localhost:8080/home/home again. The same handling is also happen with localhost:8080/about/home and both address load the same home.jsp.
yup i using localhost:8080/web_main/home.jsp to open it first time. Then 2nd tiem submit becomes localhost:8080/web_main/home/home.jsp and 3rt time localhost:8080/web_main/home/home/home.jsp error 404/ web_main is the filder i created (project name)
i got share u the web,xml but i removed it since not using since java i used for anotation based. But i got share u we,xml i use previously before deletetion
|
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.