簡體   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