在Spring MVC中找不到HTTP错误404资源

时间:2018-11-14 07:56:13

标签: java xml spring spring-mvc servlets

web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<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_3_0.xsd" version="3.0"> 
 <display-name>ValidationForm</display-name> 
<servlet> 
<servlet-name>spring-dispatcher</servlet-name> 
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>spring-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> spring-dispatcher-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <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_3_0.xsd" version="3.0"> 
 <display-name> ValidationForm</display-name> <servlet> 
<servlet-name>spring-dispatcher</servlet-name> 
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> 
<servlet-mapping> 
<servlet-name>spring-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 
</web-app>

index.jsp

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<form action="/wel" method="post"> 
name:<input type="text" name="name" placeholder="UserName"> 
<br> password:<input type="password" name="pass" placeholder="Password"> 
<br> 
<input type="submit" value="login"> 
</form> 
</body> 
</html>

HomeController.java

    package jbr.springmvc.controller; 
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 
    import org.springframework.stereotype.Controller; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.bind.annotation.RequestMethod; 
    import org.springframework.web.servlet.ModelAndView; 

    @Controller public class HomeController { 
    @RequestMapping(value = "demo1/wel", method = RequestMethod.POST)
    public ModelAndView hello(HttpServletRequest req,HttpServletResponse res) { 

String name=req.getParameter("name");
String pass=req.getParameter("pass"); 

if(name.equals("mohit")&&pass.equals("admin")) { 
String messege="Welcome"+name; 

return new ModelAndView("welcomepage","message",messege); 
} 
else 
{ return new ModelAndView("errorpage","message","Authantication Failed"); 
} 
} }

在这将显示错误404资源未找到,我真的不知道我在春季刚进入这个becoz发生了什么。请帮助大家。 预先感谢。

2 个答案:

答案 0 :(得分:-1)

当我检查此代码是否正常工作时:

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>ValidationForm</display-name>


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

</servlet>
<servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc.xsd
    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">

<annotation-driven />
<context:component-scan base-package="com.demo.spring" />
<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

HomeController:

package com.demo.spring.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView welcome() {
    return new ModelAndView("welcome");
}

@RequestMapping(value = "demo1/wel", method = RequestMethod.POST)
public ModelAndView hello(HttpServletRequest req, HttpServletResponse res) {

    String name = req.getParameter("name");
    String pass = req.getParameter("pass");

    if (name.equals("mohit") && pass.equals("admin")) {
        String messege = "Welcome" + name;

        return new ModelAndView("welcome", "message", messege);
    } else {
        return new ModelAndView("errorpage", "message", "Authantication Failed");
    }
    }
}

尝试使用它

答案 1 :(得分:-1)

You need to configure your view resolver.

1. first create spring-dispatcher.xml file.
2. Add view resolver configuration in your xml file.
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
3. place jsp file in views folder under webapp--> WEB-INF.
4. Now view resolver will find jsp at this path  " /WEB-INF/views/yoursjspname.jsp ".