欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Spring框架實(shí)現(xiàn)用戶登錄

 更新時(shí)間:2022年09月06日 09:15:20   作者:聰明的老虎  
這篇文章主要為大家詳細(xì)介紹了使用Spring框架實(shí)現(xiàn)用戶登錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了用Spring框架實(shí)現(xiàn)用戶登錄的具體代碼,供大家參考,具體內(nèi)容如下

流程:用戶登錄需求

登錄頁(yè)面login.jsp>>輸入用戶名username和密碼password,如果用戶名和密碼在數(shù)據(jù)庫(kù)中能找到,則實(shí)現(xiàn)登錄成功界面hello.jsp,不成功則跳轉(zhuǎn)到失敗頁(yè)面error.jsp

1.創(chuàng)建項(xiàng)目架構(gòu)

(1)創(chuàng)建Maven項(xiàng)目

Add Maven Property >> Name:archetypeCatalog >> value:internal

(2)添加本地?cái)?shù)據(jù)倉(cāng)庫(kù)

(3)創(chuàng)建項(xiàng)目目錄

1)groupId:指代公司名 >> com.zzx >> artifactId:指代項(xiàng)目名 >> spring_login

2)在項(xiàng)目根目錄底下創(chuàng)建文件夾target;

3)在src >> main >> java 設(shè)置為Sources Root; src >> main >> resources 設(shè)置為Resources Root;

4)加載Pom.xml文件架包,一般此文件在實(shí)際研發(fā)中都是直接由架構(gòu)師來(lái)完成的操作。一般需要注意架包版本的一致性,文件版本。

5)關(guān)于數(shù)據(jù)庫(kù)設(shè)計(jì):tb_user
uid(用戶編號(hào)),username(用戶名),password(用戶密碼),tid(用戶類型)。

resources >> db.properties

model、entity >> bean >> User

Spring核心配置文件applicationContext.xml

2.以下為具體代碼:

Spring核心配置文件 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: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">

? ? <!-- 1.加載db.properties取到有效參數(shù) -->
? ? <context:property-placeholder location="classpath:db.properties" />
? ? <!-- 2.加載數(shù)據(jù)源 -->
? ? <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
? ? ? ? <property name="driverClassName" value="${driverClass}" /> <!-- EL表達(dá)式,JSTL -->
? ? ? ? <property name="url" value="${url}" />
? ? ? ? <property name="username" value="${user}" />
? ? ? ? <property name="password" value="${password}" />
? ? </bean>
? ? <!-- 3.創(chuàng)建jdbcTemplate對(duì)象 -->
? ? <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
? ? ? ? <property name="dataSource" ref="dataSource" />
? ? </bean>

? ? <!-- 4.開(kāi)啟注解作用 -->
? ? <context:component-scan base-package="com.zzx" />
? ? <bean id="userDao" class="com.zzx.dao.impl.UserDaoImpl" />
? ? <bean id="userService" class="com.zzx.service.impl.UserServiceImpl" />

</beans>

連接數(shù)據(jù)庫(kù) db.properties

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/db_java1ssm?useSSL=true&characterEncoding=utf-8
user=root
password=123456?

webapp下配置web.xml文件

<!DOCTYPE web-app PUBLIC
? ? ? ? "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
? ? ? ? "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
? ? <display-name>Archetype Created Web Application</display-name>

? ? <!-- 1.配置applicationContext.xml -->
? ? <context-param>
? ? ? ? <param-name>contextConfigLocation</param-name>
? ? ? ? <param-value>classpath*:applicationContext.xml</param-value>
? ? </context-param>

? ? <!-- 2.監(jiān)聽(tīng)器 -->
? ? <listener>
? ? ? ? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
? ? </listener>


</web-app>

jsp頁(yè)面

<%--login.jsp--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
? ? <title>用戶登錄</title>
</head>
<body>
<div id="content" align="center">
? ? ? ? <h3>用戶登錄</h3>
? ? ? ? <hr>

? ? ? ? <form action="loginServlet.do" method="post">

? ? ? ? <table border="1" cellpadding="0" cellspacing="0" width="300px">
? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? <td><label for="username">用戶名:</label></td>
? ? ? ? ? ? ? ? <td><input type="text" id="username" name="username"/></td>
? ? ? ? ? ? </tr>
? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? <td><label for="password">用戶密碼:</label></td>
? ? ? ? ? ? ? ? <td><input type="text" id="password" name="password"/></td>
? ? ? ? ? ? </tr>
? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? <td colspan="2"> <input type="submit" name="submit" id="submit" value="登錄"/></td>
? ? ? ? ? ? </tr>
? ? ? ? </table>

? ? </form>
</div>
</body>
</html>

<%--hello.jsp--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
? ? <title>登錄成功頁(yè)面</title>
</head>
<body>

<font>歡迎您,${user.username}</font>

</body>
</html>

<%--error.jsp--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
? ? <title>登錄失敗頁(yè)面</title>
</head>
<body>

<font>登錄失?。≌?qǐng)你重新登錄!</font>

</body>
</html>

bean層User類

public class User {
? ? private int uid;
? ? private String username;
? ? private String password;
? ? private int tid;

? ? public int getUid() {
? ? ? ? return uid;
? ? }

? ? public void setUid(int uid) {
? ? ? ? this.uid = uid;
? ? }

? ? public String getUsername() {
? ? ? ? return username;
? ? }

? ? public void setUsername(String username) {
? ? ? ? this.username = username;
? ? }

? ? public String getPassword() {
? ? ? ? return password;
? ? }

? ? public void setPassword(String password) {
? ? ? ? this.password = password;
? ? }

? ? public int getTid() {
? ? ? ? return tid;
? ? }

? ? public void setTid(int tid) {
? ? ? ? this.tid = tid;
? ? }

? ? public User() {
? ? }

? ? public User(int uid, String username, String password, int tid) {
? ? ? ? this.uid = uid;
? ? ? ? this.username = username;
? ? ? ? this.password = password;
? ? ? ? this.tid = tid;
? ? }

? ? public User(String username, String password, int tid) {
? ? ? ? this.username = username;
? ? ? ? this.password = password;
? ? ? ? this.tid = tid;
? ? }

? ? @Override
? ? public String toString() {
? ? ? ? return "User{" +
? ? ? ? ? ? ? ? "uid=" + uid +
? ? ? ? ? ? ? ? ", username='" + username + '\'' +
? ? ? ? ? ? ? ? ", password='" + password + '\'' +
? ? ? ? ? ? ? ? ", tid=" + tid +
? ? ? ? ? ? ? ? '}';
? ? }
}

dao層UserDao

@Component
public interface UserDao {
? ? //用戶登錄
? ? User doLogin(String username,String password);
}

UserDaoImpl

@Component
public class UserDaoImpl implements UserDao {

? ? @Autowired
? ? private JdbcTemplate jdbcTemplate;
? ? public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
? ? ? ? this.jdbcTemplate = jdbcTemplate;
? ? }
? ? @Override
? ? public User doLogin(String username, String password) {
? ? ? ? String sql = "select * from tb_user where username=? and password=?";

? ? ? ? BeanPropertyRowMapper<User> mapper = new BeanPropertyRowMapper<>(User.class);
? ? ? ? User user = null;
? ? ? ? user = jdbcTemplate.queryForObject(sql, mapper, username, password);

? ? ? ? return user;
? ? }
}

service層UserService

@Service

public interface UserService {

? ? //用戶登錄
? ? User doLogin(String username, String password);
}

UserServiceImpl

@Service
public class UserServiceImpl implements UserService {

? ? @Autowired
? ? private UserDao userDao ;

? ? public void setUserDao(UserDao userDao) {
? ? ? ? this.userDao = userDao;
? ? }

? ? @Override
? ? public User doLogin(String username, String password) {
? ? ? ? return userDao.doLogin(username,password);
? ? }
}

LoginServlet

@WebServlet("*.do")
public class LoginServlet extends javax.servlet.http.HttpServlet {
? ? private static final long serialVersionUID =1L;

? ? // 報(bào)錯(cuò)是因?yàn)檫@個(gè)里面少tomcat - 加載tomcat
? ? protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

? ? ? ? // 中文亂碼問(wèn)題 : 國(guó)際標(biāo)準(zhǔn)化
? ? ? ? request.setCharacterEncoding("utf-8");
? ? ? ? response.setContentType("text/html;charset=UTF-8");

? ? ? ? // applicationContext.xml
? ? ? ? ApplicationContext ioc = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
? ? ? ? // 獲取用戶名
? ? ? ? String username = request.getParameter("username");
? ? ? ? String password = request.getParameter("password");

? ? ? ? UserService userService = (UserService) ioc.getBean("userService");

? ? ? ? User user = userService.doLogin(username, password);
? ? ? ? request.setAttribute("user",user);

? ? ? ? if(user==null){
? ? ? ? ? ? // 失敗
? ? ? ? ? ? request.getRequestDispatcher("error.jsp").forward(request,response);
? ? ? ? }else{
? ? ? ? ? ? request.getRequestDispatcher("hello.jsp").forward(request,response);
? ? ? ? }
? ? }

? ? protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
? ? ? ? this.doPost(request,response);
? ? }
}

最后配置一下Tomcat服務(wù)器運(yùn)行

下面是我的運(yùn)行截圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論