0

I am a beginner in Spring following an online tutorial, I have come across this issue "No mapping found for HTTP request with URI [/DemoProject/processFormGreetingTwo] in DispatcherServlet with name 'dispatcher' " on Submmitting a form. As per my understanding the URI is correct This is my project structure. Tomcat server is opening up the URL "http://localhost:8080/DemoProject/processFormGreetingTwo?userName=amy" and giving Error 404.

This is my Controller:

@Controller
public class MembersController {

    // show initial form
    @RequestMapping("/showForm")
    public String showForm() {
        return "mermbername-form";
        
    }

    // process the form
    @RequestMapping("/processForm")
    public String processForm() {
        return "welcomemember";
        
    }
    
    @RequestMapping("/processFormGreeting")
    public String greetMember(HttpServletRequest request, Model model) {
        String uName= request.getParameter("userName");
        uName= uName.toUpperCase();
        String result= "Hi! "+uName+" Have a great day!";
        model.addAttribute("message",result);
        return "welcomemember";
        
    }
    
    @RequestMapping("/processFormGreetingTwo")
    public String greetMemberTwo(@RequestParam("userName") String uName, Model model) {
        //String uName= request.getParameter("userName");
        uName= uName.toUpperCase();
        String result= "Hi! "+uName+" of BigHit Labels. Have a great day!";
        model.addAttribute("message",result);
        return "welcomemember";
        
    }
}

This is membername-form.jsp, which has the form:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>Big Hit Labels - Input Form
</head>
<body>
<c:set var="root" value="${pageContext.request.contextPath}"/>
    <form action="${root}/processFormGreetingTwo" method="GET">
        <input type="text" name="userName" placeholder="Enter your name" /> <input
            type="submit" />
    </form>
</body>
</html>

spring-mvc-demo-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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    
    <!-- Step 3: Add support for component scanning -->
    <context:component-scan
        base-package="com.bighitlabels.springdemo" />
        
        <mvc:annotation-driven />

    <!-- Step 4: Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven />

    <!-- Step 5: Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

and finally, web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">

    <display-name>spring-mvc-demo</display-name>

    <absolute-ordering />

    <!-- Spring MVC Configs -->

    <!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>

On submitting the form , I get expected results for Requests "/ProcessForm" and "/ProcessFormGreeting" but Error 404 for "/ProcessFormGreetingTwo". Please help me figure out where I could be going wrong with this particular request?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

0 Answers0