解决 Path with "WEB-INF" or "META-INF": [WEB-INF/views/index.jsp] 问题

解决 Path with "WEB-INF" or "META-INF": [WEB-INF/views/index.jsp] 问题

在SpringBoot项目中配置使用JSP后运行项目没有任何报错,但是当登录页面的时候会报404,如图:
在这里插入图片描述

在这里插入图片描述
然后发现后台输出只是打印了Path with “WEB-INF” or “META-INF”: [WEB-INF/views/index.jsp]
在这里插入图片描述

首先可以简单的测试一下,是不是Controller的问题,是不是地址映射问题
在UserController中添加一个输出语句,重启项目后,重新登录页面,查看控制台是否有输出,如果有输出,则表示Controller层没有问题,这就表示可能是依赖有问题

在这里插入图片描述

检查build.gradle 文件有无问题,重新导入依赖
在这里插入图片描述

页面正常打开
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是实现上述功能的步骤: 1. 创建一个SpringMVC项目,可以使用Spring Tool Suite等IDE工具来创建。 2. 在webapp目录下创建login.jsp页面,该页面包含一个表单,可以输入用户名和密码,并且有一个提交按钮。 3. 在web.xml文件中配置DispatchServlet,将其映射到“/”路径下。 4. 在Spring-mvc.xml(applicationContext.xml)文件中配置Controller和视图解析器。 5. 编写Controller类,其中包含userlogin方法,该方法接收用户输入的用户名和密码,并进行相应的处理。 6. 在userlogin方法中,可以使用HttpServletRequest对象来获取用户输入的用户名和密码。 7. 如果用户名和密码是ZhangSan和123时,则跳转到main.jsp页面,并显示用户名在页面上。否则,跳转到login.jsp页面,并显示用户名或密码错误。 8. 在main.jsp页面中,显示用户输入的用户名。 下面是实现上述功能的代码示例: 1. login.jsp页面 ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Login Page</title> </head> <body> <h2>Login Page</h2> <form action="${pageContext.request.contextPath}/userlogin" method="post"> <label for="userName">UserName:</label> <input type="text" id="userName" name="userName"/><br/><br/> <label for="password">Password:</label> <input type="password" id="password" name="password"/><br/><br/> <input type="submit" value="Login"/> </form> </body> </html> ``` 2. Spring-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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <context:component-scan base-package="com.example.controller" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans> ``` 3. Controller类 ``` package com.example.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class UserController { @RequestMapping(value = "/login", method = RequestMethod.GET) public String showLoginPage() { return "login"; } @RequestMapping(value = "/userlogin", method = RequestMethod.POST) public String userLogin(HttpServletRequest request, Model model) { String userName = request.getParameter("userName"); String password = request.getParameter("password"); if (userName.equals("ZhangSan") && password.equals("123")) { model.addAttribute("userName", userName); return "main"; } else { model.addAttribute("errorMsg", "Username or password is incorrect"); return "login"; } } } ``` 4. main.jsp页面 ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Main Page</title> </head> <body> <h2>Welcome ${userName}!</h2> </body> </html> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值