gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:19:56 25 4
gpt4 key购买 nike

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

它不是以下内容的副本: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>

config-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 )

@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

最佳答案

试试这个:

<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-pattern 的内容更改为:/*

关于java - Tomcat 在尝试登录时给出 HTTP 状态 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45089202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com