亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

首先,我們得記得spring-MVC是通過(guò)三個(gè)層次和Spring對(duì)項(xiàng)目進(jìn)行調(diào)用,本次我構(gòu)建的簡(jiǎn)單登錄程序主要構(gòu)筑如下

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

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層設(shè)計(jì)接口

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

然后我們?cè)趍apper包下寫.xml配置文件對(duì)接口類中的方法進(jìn)行實(shí)現(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>

然后本次簡(jiǎn)單的實(shí)現(xiàn)登錄功能所以不使用業(yè)務(wù)層而直接使用控制層對(duì)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", "賬號(hào)或密碼錯(cuò)誤");
? ? ? ? ? ? return "index";
? ? ? ? }
? ? ? ? return "index";
? ? }

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

}

這里詳細(xì)標(biāo)簽的使用讀者可以自行百度,最后是jsp頁(yè)面的實(shí)現(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">
? ? 賬號(hào):<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文件可以自行設(shè)計(jì),無(wú)太大影響。其余配置文件如下

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ù)庫(kù)相關(guān)參數(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對(duì)象 -->
? ? <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
? ? ? ? <!-- 注入數(shù)據(jù)庫(kù)連接池 -->
? ? ? ? <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>

? ? <!-- 自動(dòng)生成dao,mapper-->
? ? <!-- 4.配置掃描Dao接口包,動(dòng)態(tài)實(shí)現(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>

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


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

? ? <!-- 解決中文亂碼過(guò)濾器 -->
? ? <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 服務(wù)器啟動(dòng)創(chuàng)建servlet) -->
? ? <servlet>
? ? ? ? <servlet-name>dispatcherServlet</servlet-name>
? ? ? ? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
? ? ? ? <!-- 配置初始化參數(shù),創(chuàng)建完DispatcherServlet對(duì)象,加載springmvc.xml配置文件 -->
? ? ? ? <init-param>
? ? ? ? ? ? <param-name>contextConfigLocation</param-name>
? ? ? ? ? ? <param-value>classpath:spring/spring-mvc.xml</param-value>
? ? ? ? </init-param>
? ? ? ? <!-- 服務(wù)器啟動(dòng)的時(shí)候,讓DispatcherServlet對(duì)象創(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ù)以本地庫(kù)為主,以上為我的數(shù)據(jù)庫(kù),記得進(jìn)行修改

以上就是我構(gòu)建的簡(jiǎn)單登錄程序,希望能幫到讀者,多謝閱讀。

希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論