SpringMVC实现简单登录

超、凢脫俗 2022-05-25 13:37 260阅读 0赞

1.基本环境:eclipse+jdk1.8+Tomcat9.0+springmvc各种jar

2.把jar导入到lib下,在build path中添加这些jar

3.创建一个server,作为运行时用。

4.目录如下:

70

index.jsp

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <form action="login.do" method="post">
  11. <label>用户名:</label><input type="text" name="username"/><br>
  12. <label>密码:</label><input type="password" name="password"/><br>
  13. <input type="submit" value="提交"/>
  14. </form>
  15. </body>
  16. </html>

succ.jsp

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <title>成功</title>
  8. </head>
  9. <body>
  10. <div>
  11. <%
  12. String name = (String)request.getAttribute("username");
  13. String pwd = (String)request.getAttribute("password");
  14. %>
  15. <p>用户名:<%=name %></p>
  16. <p>密码:<%=pwd %></p>
  17. </div>
  18. </body>
  19. </html>

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  11. </beans>

springmvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  8. <!-- 启用spring mvc注解 -->
  9. <context:component-scan base-package="com.chunyan.spring"></context:component-scan>
  10. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  11. <property name="prefix" value="/"></property>
  12. <property name="suffix" value=".jsp"></property>
  13. </bean>
  14. <!-- 在名为'springmvc'的DispatcherServlet中找不到具有URI [/ThirdPage/login.do]的HTTP请求的映射 -->
  15. <!-- 在实际开发中通常都需配置 mvc:annotation-driven 标签 -->
  16. <mvc:annotation-driven></mvc:annotation-driven>
  17. </beans>

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <display-name>ThirdPage</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.jsp</welcome-file>
  6. </welcome-file-list>
  7. <listener>
  8. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  9. </listener>
  10. <context-param>
  11. <param-name>contextConfigLocation</param-name>
  12. <param-value>classpath*:applicationContext.xml</param-value>
  13. </context-param>
  14. <servlet>
  15. <servlet-name>springmvc</servlet-name>
  16. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  17. <init-param>
  18. <param-name>contextConfigLocation</param-name>
  19. <param-value>/WEB-INF/classes/springmvc.xml</param-value>
  20. </init-param>
  21. <load-on-startup>1</load-on-startup>
  22. </servlet>
  23. <servlet-mapping>
  24. <servlet-name>springmvc</servlet-name>
  25. <url-pattern>*.do</url-pattern>
  26. </servlet-mapping>
  27. </web-app>

Usercontroller.java

  1. package com.chunyan.spring;
  2. import javax.servlet.http.HttpServletRequest;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. @Controller
  6. public class UserController{
  7. @RequestMapping("/login")
  8. public String login(String username, String password, HttpServletRequest request) {
  9. System.out.println("用户名:"+username+"\t密码:"+password);
  10. request.setAttribute("username", username);
  11. request.setAttribute("password", password);
  12. return "succ";
  13. }
  14. }

运行结果:

70 1

70 2

发表评论

表情:
评论列表 (有 0 条评论,260人围观)

还没有评论,来说两句吧...

相关阅读