在webapp下创建views/*.jsp页面

创建index.jsp

 
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here </title> </head> <body> This is index page. </body> </html>
创建hello.jsp
 
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here </title> </head> <body> <h1>Hello World! </h1> </body> </html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现上述功能的步骤: 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> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值