當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringMVC实现简单登录
生活随笔
收集整理的這篇文章主要介紹了
SpringMVC实现简单登录
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.基本環(huán)境:eclipse+jdk1.8+Tomcat9.0+springmvc各種jar
2.把jar導(dǎo)入到lib下,在build path中添加這些jar
3.創(chuàng)建一個server,作為運行時用。
4.目錄如下:
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body><form action="login.do" method="post"><label>用戶名:</label><input type="text" name="username"/><br><label>密碼:</label><input type="password" name="password"/><br><input type="submit" value="提交"/></form> </body> </html>succ.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>成功</title> </head> <body><div><%String name = (String)request.getAttribute("username");String pwd = (String)request.getAttribute("password");%><p>用戶名:<%=name %></p><p>密碼:<%=pwd %></p></div> </body> </html>applicationContext.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:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> </beans>springmvc.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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"><!-- 啟用spring mvc注解 --><context:component-scan base-package="com.chunyan.spring"></context:component-scan><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean> <!-- 在名為'springmvc'的DispatcherServlet中找不到具有URI [/ThirdPage/login.do]的HTTP請求的映射 --><!-- 在實際開發(fā)中通常都需配置 mvc:annotation-driven 標(biāo)簽 --><mvc:annotation-driven></mvc:annotation-driven> </beans>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>ThirdPage</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:applicationContext.xml</param-value></context-param><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping> </web-app>Usercontroller.java
package com.chunyan.spring;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class UserController{@RequestMapping("/login")public String login(String username, String password, HttpServletRequest request) {System.out.println("用戶名:"+username+"\t密碼:"+password); request.setAttribute("username", username);request.setAttribute("password", password);return "succ";} }運行結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的SpringMVC实现简单登录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BIEE表头制作
- 下一篇: fastjson解析多层数据_JSON数