Spring RequestMapping 404 status

Well, I know that there are 20 posts with the same problem here, but none of them seem to help me, so this will probably be a duplicate, but I went to all the other posts, and none of them solved mine the problem, so there must be something that I am doing wrong, or I am not making the right changes from the answers of the previous questions.

I'm trying to make a small application using Spring, and I'm still experimenting with it, but I spent 4 days trying to figure out what happened, and I just can't. I am still getting HTTP 404 status when I try to return jsp from the controller. Nothing but 404 status through Tomcat, nothing more ...

WebAppController:

@Controller public class WebAppController { @RequestMapping(value="/login", method = RequestMethod.GET) public String login() { System.out.println("You have entered the login maprequest"); return "test1"; } } 

web.xml:

  <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_2_5.xsd" version="2.5"> <display-name>Hotel Application</display-name> <servlet> <servlet-name>WebApp</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>WebApp</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app> 

webApp.xml:

  <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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:component-scan base-package="com.iquestgroup" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans> 

This configuration works in a simple maven project with just the one mentioned above. The problem is that the same does not work in a maven project with 3 modules (persistence, service and webapp). I copied the same thing in webapp, and when I launched it on the server, I got the status of 404 http ... although the modules were successfully built.

LE The first part of the accepted answer relates to a common servlet mapping error made by those starting with Spring. My problem was not related to this, and I ended up deleting it after the initial answer. In order not to confuse readers, the first part of the accepted answer relates to the following code:

 <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> 
+6
source share
2 answers

Change

 <url-pattern>/*</url-pattern> 

to

 <url-pattern>/</url-pattern> 

Your currently displayed DispatcherServlet marked as processing all requests due to /* . This means that he will also try to process the request sent to /WEB-INF/jsp/test1.jsp . Obviously there is no handler for this, and it will fail.

Sample / differs in that it is the default backup if another template does not match. If there is a Servlet that appears in the request path, then a Servlet will be selected before your DispatcherServlet displayed.

Most (possibly all) servlet containers display a mapping to Servlet for processing JSP packets using url-pattern *.jsp . This will take precedence over your DispatcherServlet mapped to / .

If you still get 404 after these changes, there are several possibilities.

Spring registers at the INFO level any methods of the handler ( @RequestMapping annotated methods in @Controller beans) that it registers. If you do not see any of your logs, your web application has not been deployed correctly / successfully.

If your Servlet container is built into your IDE, check the appropriate tab / view in the applications that are deployed. If it is standalone, check that your generated .war file is located in the appropriate directory and is correctly expanded.

+6
source

First of all, you can listen / query any root controller, as shown below.

 @Controller @RequestMapping ("/") public class RootCtrl { Logger logger = Logger.getLogger(RootCtrl.class); @RequestMapping (value = "/", method = {RequestMethod.GET, RequestMethod.POST}) public String index(ModelMap model) { // redirect to login page return "redirect:admin/index"; }} 

With this controller, you will display all requests in root. Then you can redirect the request to your login controller.

All about query matching. Your loginController or webController send a listen to the request, which must be specified earlier. In my application, the login controller listens / manages requests. Than my login controller looks like this:

 @Controller @RequestMapping ("/admin") public class LoginCtrl { @RequestMapping (value = "/index", method = {RequestMethod.GET, RequestMethod.POST}) public String index(@RequestParam (value = "captchaId", defaultValue = "") String captchaId, ModelMap model, HttpServletRequest request, HttpServletResponse response) { if(StringUtils.isNullOrEmpty(captchaId)){ captchaId = RandomGUID.getRandomGUID(); } model.addAttribute("captchaId", captchaId); // redirect to login page return "login"; } 

When you get a request along this path: localhost / SampleProject / admin / index. Mappings will work.

+1
source

Source: https://habr.com/ru/post/978948/


All Articles