繁体   English   中英

Tomcat在尝试登录时给出HTTP状态404

[英]Tomcat gives HTTP Status 404 when trying to log in

请在将其设置为重复之前先阅读!

它不是以下内容的重复: HTTP状态404-请求的资源(/)不可用,我已经尝试过此资源,并且在此站点中已尝试过有关此主题的所有其他主题,但没有帮助。

我正在用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;

}
}

我的项目的结构如下:

图像描述入门

编辑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

尝试这个:

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

该站点上的代码编辑器弄乱了格式,但将url模式的内容更改为:/ *

将其添加到表单声明中的操作中:

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

        }

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM