1

I am new with spring and I should do the project without Boot. Maybe you will see what I am doing wrong.

I should get a note on console "working". But instead of I just getting an error when my URL is on: http://localhost:8081/UserService/working?.

I think their problem with servlet mapping. @RequestMapping cant be seen.

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">

 <display-name>Archetype Created Web Application</display-name>
 
 <!-- Spring MVC - START -->
 <!-- Processes application requests -->
 <servlet>
  <servlet-name>alwaysUseFullPath</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/servlet-context.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>alwaysUseFullPath</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 
 
 <!-- Spring MVC - END -->
 
 <!-- The definition of the Root Spring Container shared by all Servlets 
  and Filters -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/servlet-context.xml</param-value>
 </context-param>
 
 <!-- Creates the Spring Container shared by all Servlets and Filters -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
</web-app>

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>


<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"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
  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">
<mvc:annotation-driven />
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="alwaysUseFullPath" value="true"/>
</bean>  


   <context:component-scan base-package = "com.xdddd" />



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

package com.xdddd;

import org.springframework.stereotype.Controller;
NewUserController

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@EnableWebMvc
@Controller
public class NewUserController {
 
 @RequestMapping("/working", method = RequestMethod.GET)
 public void working() {
  System.out.println("Working");
 }

}

html


<html>
<body>
<form action="working">
<input type="text">Email<br>
<input type="text">Name<br>
<input type="text">Password<br><br>
<button>Submit</button>
</form>
</body>
</html>

At the end I am getting:

WARNING: No mapping found for HTTP request with URI [/UserService/working] in DispatcherServlet with name 'alwaysUseFullPath'

fool-dev
  • 7,671
  • 9
  • 40
  • 54

1 Answers1

0

You can map more specific URL pattern like *.html, .do, /pages/, /app/*, etc. For example in your code :

<url-pattern>*.do</url-pattern> or <url-pattern>*.html</url-pattern>

<servlet>
  <servlet-name>alwaysUseFullPath</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/servlet-context.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>alwaysUseFullPath</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>

In Spring controller the method request mapping patten looks like this :

/working.do or /working.html

@EnableWebMvc
@Controller
public class NewUserController {
 
 @RequestMapping("/working.do", method = RequestMethod.GET)
 public void working() {
  System.out.println("Working");
 }

}

This is usually we do to hide actually mapping from end user.

Ramesh Fadatare
  • 561
  • 4
  • 12