最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring?MVC實現(xiàn)簡單登錄功能

 更新時間:2022年09月06日 09:36:10   作者:橘式  
這篇文章主要為大家詳細介紹了spring?MVC實現(xiàn)簡單登錄功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

spring-MVC實現(xiàn)簡單的登錄功能,供大家參考,具體內(nèi)容如下

今天我學習了spring-MVC實現(xiàn)簡單的登錄功能,本篇博客就講解如何使用spring-MVC實現(xiàn)簡單的登錄功能

首先,我們得記得spring-MVC是通過三個層次和Spring對項目進行調(diào)用,本次我構建的簡單登錄程序主要構筑如下

在entity下建立User類對數(shù)據(jù)進行管理`

public class User {
? ? private Integer id;
? ? private String username;
? ? private String password;

? ? public Integer getId() {
? ? ? ? return id;
? ? }

? ? public void setId(Integer id) {
? ? ? ? this.id = id;
? ? }

? ? 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;
? ? }

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

然后在再在dao層設計接口

import com.zhongruan.web_demo.entity.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

public interface IUserDao {

? ? List<User> getUsers();

? ? User getUserByName(@Param("name") String username);

? ? User getUserById2(String username);

? ? int updateUser(User user);

? ? int addUser(User user);

? ? int deleteUser(User user);
}

然后我們在mapper包下寫.xml配置文件對接口類中的方法進行實現(xiàn)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zhongruan.web_demo.dao.IUserDao">
? ? <select id="getUserByName" resultType="User">
? ? ? ? select * from tb_user where username = #{name}
? ? </select>
</mapper>

然后本次簡單的實現(xiàn)登錄功能所以不使用業(yè)務層而直接使用控制層對dao層直接調(diào)用,controller層的類定義如下

import com.zhongruan.web_demo.dao.IUserDao;
import com.zhongruan.web_demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
? ? @Autowired
? ? IUserDao userDao;
? ? @RequestMapping("/demo")
? ? public String toDemo(Model model) {
? ? ? ? model.addAttribute("users", userDao.getUsers());
? ? ? ? return "user-list";
? ? }

? ? @RequestMapping("/logintest")
? ? public String Logintest(Model model,User user, @RequestParam("username") String name,
? ? ? ? ? ? ? ? ? ? ? ? ? ? @RequestParam("password") String password)
? ? {
? ? ? ? User user1 = userDao.getUserByName(name);
? ? ? ? if (user1 == null) {
? ? ? ? ? ? model.addAttribute("msg", "不存在該用戶");
? ? ? ? ? ? return "index";
? ? ? ? }
? ? ? ? if (!user.getPassword().equals(password)) {
? ? ? ? ? ? model.addAttribute("msg", "賬號或密碼錯誤");
? ? ? ? ? ? return "index";
? ? ? ? }
? ? ? ? return "index";
? ? }

? ? @RequestMapping("/returnmenu")
? ? public String returnmenu(){
? ? ? ? return "index";
? ? }

}

這里詳細標簽的使用讀者可以自行百度,最后是jsp頁面的實現(xiàn)

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/jsp/taglibs.jsp" %>

<html>
<head>
? ? <title>${basePath}</title>
</head>
<body>
<form style="width: 300px;margin: 200px auto 0;" method="post" action="${basePath}/logintest">
? ? 賬號:<input type="text" name="username" value="${user.username}">
? ? <br>
? ? 密碼:<input type="password" name="password" value="${user.password}">
? ? <br>
? ? <input type="submit" value="登錄">
? ? <br>
? ? <span style="color: red;">${msg}</span>
</form>
</body>
</html>

index.jsp文件可以自行設計,無太大影響。其余配置文件如下

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"
? ? ? ?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.注解掃描位置-->
? ? <context:component-scan base-package="com.zhongruan.web_demo.controller" />

? ? <!-- 2.配置映射處理和適配器-->
? ? <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
? ? <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

? ? <!-- 3.視圖的解析器-->
? ? <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
? ? ? ? <property name="prefix" value="/WEB-INF/jsp/" />
? ? ? ? <property name="suffix" value=".jsp" />
? ? </bean>
</beans>

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"
? ? ? ?xmlns:aop="http://www.springframework.org/schema/aop"
? ? ? ?xmlns:tx="http://www.springframework.org/schema/tx"
? ? ? ?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
?? ??? ?http://www.springframework.org/schema/tx
?? ??? ?http://www.springframework.org/schema/tx/spring-tx.xsd">
? ? <!-- 1.配置數(shù)據(jù)庫相關參數(shù)properties的屬性:${url} -->
? ? <context:property-placeholder location="classpath:properties/db.properties"/>

? ? <!-- 2.配置數(shù)據(jù)源 -->
? ? <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
? ? ? ? <property name="driverClass" value="${jdbc.driver}"/>
? ? ? ? <property name="jdbcUrl" value="${jdbc.url}"/>
? ? ? ? <property name="user" value="${jdbc.username}"/>
? ? ? ? <property name="password" value="${jdbc.password}"/>
? ? ? ? <property name="maxPoolSize" value="30"/>
? ? ? ? <property name="minPoolSize" value="2"/>
? ? </bean>

? ? <!-- 3.配置SqlSessionFactory對象 -->
? ? <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
? ? ? ? <!-- 注入數(shù)據(jù)庫連接池 -->
? ? ? ? <property name="dataSource" ref="dataSource"/>
? ? ? ? <!-- 掃描bean包 使用別名 -->
? ? ? ? <property name="typeAliasesPackage" value="com.zhongruan.web_demo"/>

? ? ? ? <!--配置加載映射文件 UserMapper.xml-->
? ? ? ? <property name="mapperLocations" value="classpath:mapper/*.xml"/>

? ? ? ? <!--配置mybatis配置文件位置-->
<!-- ? ? ? ?<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>-->

? ? </bean>

? ? <!-- 自動生成dao,mapper-->
? ? <!-- 4.配置掃描Dao接口包,動態(tài)實現(xiàn)Dao接口,注入到spring容器中 -->
? ? <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
? ? ? ? <!-- 給出需要掃描Dao接口包 -->
? ? ? ? <property name="basePackage" value="com.zhongruan.web_demo.dao"/>
? ? ? ? <!-- 注入sqlSessionFactory -->
? ? ? ? <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
? ? </bean>

? ? <!--自動掃描-->
? ? <context:component-scan base-package="com.zhongruan.web_demo"/>


? ? <!-- 配置事務-->
? ? <!-- 5.配置事務管理器 -->
? ? <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
? ? ? ? <property name="dataSource" ref="dataSource"/>
? ? </bean>
? ? <!-- 6.開啟事務注解-->
? ? <tx: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"
? ? ? ? ?version="3.1">

? ? <!-- 配置加載類路徑的配置文件 -->
? ? <context-param>
? ? ? ? <param-name>contextConfigLocation</param-name>
? ? ? ? <param-value>classpath*:spring/applicationContext.xml</param-value>
? ? </context-param>

? ? <!-- 配置監(jiān)聽器 -->
? ? <listener>
? ? ? ? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
? ? </listener>
? ? <listener>
? ? ? ? <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
? ? </listener>

? ? <!-- 解決中文亂碼過濾器 -->
? ? <filter>
? ? ? ? <filter-name>characterEncodingFilter</filter-name>
? ? ? ? <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
? ? ? ? <init-param>
? ? ? ? ? ? <param-name>encoding</param-name>
? ? ? ? ? ? <param-value>UTF-8</param-value>
? ? ? ? </init-param>
? ? </filter>
? ? <filter-mapping>
? ? ? ? <filter-name>characterEncodingFilter</filter-name>
? ? ? ? <url-pattern>/*</url-pattern>
? ? </filter-mapping>

? ? <!-- 前端控制器(加載classpath:spring-mvc.xml 服務器啟動創(chuàng)建servlet) -->
? ? <servlet>
? ? ? ? <servlet-name>dispatcherServlet</servlet-name>
? ? ? ? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
? ? ? ? <!-- 配置初始化參數(shù),創(chuàng)建完DispatcherServlet對象,加載springmvc.xml配置文件 -->
? ? ? ? <init-param>
? ? ? ? ? ? <param-name>contextConfigLocation</param-name>
? ? ? ? ? ? <param-value>classpath:spring/spring-mvc.xml</param-value>
? ? ? ? </init-param>
? ? ? ? <!-- 服務器啟動的時候,讓DispatcherServlet對象創(chuàng)建 -->
? ? ? ? <load-on-startup>1</load-on-startup>
? ? </servlet>
? ? <servlet-mapping>
? ? ? ? <servlet-name>dispatcherServlet</servlet-name>
? ? ? ? <url-pattern>/</url-pattern>
? ? </servlet-mapping>
</web-app>
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/people_manage
jdbc.username=root
jdbc.password=123456
//其中數(shù)據(jù)以本地庫為主,以上為我的數(shù)據(jù)庫,記得進行修改

以上就是我構建的簡單登錄程序,希望能幫到讀者,多謝閱讀。

希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 解析rainbond以應用為中心的架構設計原理

    解析rainbond以應用為中心的架構設計原理

    這篇文章主要為大家介紹了rainbond以應用為中心的架構設計實現(xiàn)及原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-02-02
  • 詳解IDEA中類加載器調(diào)用getResourceAsStream()方法需注意的問題

    詳解IDEA中類加載器調(diào)用getResourceAsStream()方法需注意的問題

    這篇文章主要介紹了詳解IDEA中類加載器調(diào)用getResourceAsStream()方法需注意的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-02-02
  • Java三大特性-封裝知識小結

    Java三大特性-封裝知識小結

    所有的面向?qū)ο缶幊陶Z言的思路都是差不多的,而這三大特性,則是思路中的支柱點,接下來我就重點講解了一下java三大特性-封裝,感興趣的朋友跟隨腳本之家小編一起看看吧
    2018-03-03
  • webuploader在springMVC+jquery+Java開發(fā)環(huán)境下的大文件分片上傳的實例代碼

    webuploader在springMVC+jquery+Java開發(fā)環(huán)境下的大文件分片上傳的實例代碼

    這篇文章主要介紹了webuploader在springMVC+jquery+Java開發(fā)環(huán)境下的大文件分片上傳的實例代碼,需要的朋友可以參考下
    2017-04-04
  • Java不可不知的泛型使用示例代碼

    Java不可不知的泛型使用示例代碼

    這篇文章主要介紹了Java不可不知的泛型使用,本文通過實例代碼給大家介紹了java的泛型的基本使用,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • 兩種用空格分隔的java字符串的方式

    兩種用空格分隔的java字符串的方式

    這篇文章主要介紹了兩種用空格分隔的java字符串的方式的方法,非常簡單實用,需要的朋友可以參考下
    2015-03-03
  • Fluent Mybatis實際開發(fā)中的優(yōu)勢對比

    Fluent Mybatis實際開發(fā)中的優(yōu)勢對比

    本文給大家介紹如何通過IQuery和IUpdate定義強大的動態(tài)SQL語句,給大家分享Fluent Mybatis實際開發(fā)中的優(yōu)勢講解,感興趣的朋友一起看看吧
    2021-08-08
  • JAVA如何把數(shù)據(jù)庫的數(shù)據(jù)處理成樹形結構

    JAVA如何把數(shù)據(jù)庫的數(shù)據(jù)處理成樹形結構

    本文介紹了JAVA如何把數(shù)據(jù)庫的數(shù)據(jù)處理成樹形結構,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • java實現(xiàn)兩個文件的拼接

    java實現(xiàn)兩個文件的拼接

    這篇文章主要為大家詳細介紹了java實現(xiàn)兩個文件的拼接,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Java 中圖片壓縮處理的解決方案

    Java 中圖片壓縮處理的解決方案

    圖片經(jīng)過base64編碼轉(zhuǎn)換后,文件會變大的原因是因為base64編碼會將每個3字節(jié)的數(shù)據(jù)轉(zhuǎn)換成4字節(jié)的數(shù)據(jù),并且在轉(zhuǎn)換的過程中還會添加一些額外的字符,這篇文章主要介紹了Java 中如何對圖片進行壓縮處理,需要的朋友可以參考下
    2023-09-09

最新評論

内黄县| 宜宾县| 平陆县| 绵竹市| 吉安市| SHOW| 辉南县| 高唐县| 铁岭市| 平度市| 克什克腾旗| 大埔县| 成安县| 陕西省| 辽阳市| 富阳市| 张家界市| 兴和县| 宝应县| 福鼎市| 西林县| 钟山县| 同江市| 新绛县| 筠连县| 乌拉特后旗| 邮箱| 永春县| 彭水| 柳州市| 响水县| 泽库县| 封开县| 鸡西市| 灵武市| 井冈山市| 霞浦县| 商城县| 唐海县| 鹤山市| 罗城|