尝试登录时,Tomcat提供HTTP状态404

时间:2017-07-13 19:11:48

标签: java spring jsp spring-mvc tomcat

请在设置之前重复阅读!

这不是重复:HTTP Status 404 - The requested resource (/) is not available我已经在本网站上尝试了这个以及所有其他相关主题而没有帮助。

我正在使用Spring MVC和Tomcat开发一个小项目,到目前为止我已经创建了登录和主页,登录后会重定向。这些是与问题相关的文件,如果有什么遗漏或需要,拜托,请告诉我。

问题是,它完美地显示了index.jsp(登录页面)但是如果我引入凭据它显示错误HTTP-404:请求的资源不可用,我找不到问题所在。如果登录正确,它应该转到main.jsp,如果没有,它应该返回index.jsp并显示错误消息。我也没有在控制台中出现任何错误。 提前谢谢。

的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>WebProject</display-name>
     <welcome-file-list>
     <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      <servlet>
      <servlet-name>SpringDispatcherServlet</servlet-name>
       <servlet-
      class>org.springframework.web.servlet.DispatcherServlet</servlet- 
    class>
   <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/config-mvc.xml</param-value>
    </init-param>
    </servlet>
    <servlet-mapping>
   <servlet-name>SpringDispatcherServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
    </servlet-mapping>
   <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:configApplication.xml</param-value>
   </context-param>
  </web-app>

的index.jsp

 <!doctype html>
 <html>
 <head>
<meta charset="utf-8" />
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<title>TPV Cocoa</title>
<style>
    .formulario{
        margin: 0 auto;
        float: none;
    }
</style>
</head>
<body>
<div class="container-fluid">
<h1 class="text-center">Inicia sesión en TPV Cocoa</h1>
<form action="login.do" method="post">
<div class="row">
    <div class="col-lg-4 col-lg-offset-4 col-sm-4 col-sm-offset-4">  
        <div class="formulario form-group">
            <div class="input-group">
                <span class="input-group-addon"><i class="glyphicon 
 glyphicon-user"></i></span>
                <input id="user" type="text" class="form-control" 
 name="user" placeholder="Usuario" value=""> 
            </div>
            <br>
            <div class="input-group">
                <span class="input-group-addon"><i class="glyphicon 
  glyphicon-lock"></i></span>
                <input id="password" type="password" class="form-control" 
  name="password" placeholder="Password" value=""> 
            </div>
            <div>
                <c:out value="${requestScope.error}"/>
            </div>
            <br>
            <input type="submit" class="btn btn-default" value="Iniciar 
     sesión"/>
        </div>
    </div>
</div>
 </form>
   <div id="avisos" style="color:red"><?php echo $avisos ?></div>
 </div>
 <script>document.getElementById('usuario').focus();</script>   
  </body>
   </html>

配置-mvc.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"
 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">

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

UserController (用于登录的控制器)

@Controller
@ComponentScan("cocoa.tpv.controllers")
public class UserController {

@Autowired
UserFacade userService;


@RequestMapping("login.do")
public ModelAndView loginUsuario(HttpServletRequest request, HttpServletResponse response) throws IOException{
    HttpSession session=request.getSession();

    ModelAndView modelAndView=new ModelAndView();
    User user=new User();

    String userName=request.getParameter("user");
    String userPassword=request.getParameter("password");

    try{
        user.setName(userName);
        user.setPassword(userPassword);

        User usuarioLogged=userService.getUser(user);

        if(usuarioLogged == null){
            modelAndView.setViewName("index.jsp");
        }else{
            modelAndView.setViewName("main.jsp");
            modelAndView.addObject("usuarioLogged", usuarioLogged);
        }

    }catch(MainException excepcion){
        modelAndView.setViewName("index.jsp");
        modelAndView.addObject("error", excepcion.getMessage());
    }

    return modelAndView;

}
}

我的项目结构如下:

introducir la descripción de la imagen aquí

编辑1:

我添加了一个视频,这样您就可以看到我的网站实际上做了什么以及问题。

https://i.gyazo.com/e5aac4b7c067247f2424d4a8cc6eb123.mp

编辑2:

我从我尝试登录的那一刻起就添加了服务器日志:

localhost_access_log2017-07-14.txt

这是我第一次登录时得到的结果:

  127.0.0.1 - - [14/Jul/2017:17:32:30 +0200] "GET / HTTP/1.1" 200 11452

这是我尝试再次登录后得到的结果:

   0:0:0:0:0:0:0:1 - - [14/Jul/2017:17:34:05 +0200] "GET /TPV/index.jsp 
   HTTP/1.1" 200 1938

   0:0:0:0:0:0:0:1 - - [14/Jul/2017:17:34:10 +0200] "POST /TPV/login.do 
   HTTP/1.1" 404 1002

2 个答案:

答案 0 :(得分:0)

试试这个:

<servlet-mapping>
<servlet-name>SpringDispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern> //Change the url pattern attribute in your web.xml to this
</servlet-mapping>

网站上的代码编辑器会弄乱格式,但会将网址格式的内容更改为:/ *

答案 1 :(得分:0)

将此内容添加到表单声明中的操作:

<form action="${pageContext.request.contextPath}/login.do" method="POST">

我修改了您的代码,请立即尝试

    @RequestMapping("login.do", method=RequestMethod.POST)
public ModelAndView loginUsuario(HttpServletRequest request, HttpServletResponse response) throws IOException{
    HttpSession session=request.getSession();

    ModelAndView modelAndView=new ModelAndView();
    User user=new User();

    String userName=request.getParameter("user");
    String userPassword=request.getParameter("password");

    try{
        user.setName(userName);
        user.setPassword(userPassword);

        User usuarioLogged=userService.getUser(user);

        if(usuarioLogged == null){
            modelAndView.setViewName("redirect:index");
        }else{
            modelAndView.setViewName("redirect:main");
            modelAndView.addObject("usuarioLogged", usuarioLogged);
        }

    }catch(MainException excepcion){
        modelAndView.setViewName("redirect:index");
        modelAndView.addObject("error", excepcion.getMessage());
    }

    return modelAndView;

}

[编辑]:我错过了删除扩展程序 希望这有帮助!

[编辑2]:

请看这个控制器,也许你可以用它作为参考

@Controller
@RequestMapping("/singup")
public class SingupController {

    SingupValidations userValidations;
    private JdbcTemplate jdbcTemplate;

    public SingupController(){
        this.userValidations = new SingupValidations();
        DBConnections DBConnection = new DBConnections();
        this.jdbcTemplate = new JdbcTemplate(DBConnection.connect());

    }

    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView singup(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("singup");
        mav.addObject("users", new User());
        return mav;
    }

    @RequestMapping(method=RequestMethod.POST)
    public ModelAndView singup(@ModelAttribute("users") User u, BindingResult result){

        this.userValidations.validate(u, result);
        if(result.hasErrors()){
            ModelAndView mav = new ModelAndView();
            mav.setViewName("singup");
            mav.addObject("users", new User());
            return mav;

        }else{
            this.jdbcTemplate.update("insert into Users (nombre, correo, telefono, password) values (?,?,?,?)", u.getUsername(), u.getCorreo(), u.getTelefono(), u.getPassword());
            ModelAndView mav = new ModelAndView();
            mav.setViewName("redirect:index");
            return mav;

        }

    }