您好,登錄后才能下訂單哦!
小編這次要給大家分享的是SSM框架下如何實現登錄注冊,文章內容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
基本配置:jdk1.8 tomcat 8 MyEclipse
先打好地基:
spring配置文件 application.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 通過注解,將Service的生命周期納入Spring的管理 --> <context:annotation-config /> <!-- 通過注解,將Service的生命周期納入Spring的管理 --> <context:component-scan base-package="service"></context:component-scan> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!-- 配置數據源 --> <property name="driverClassName"> <value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value> </property> <property name="url"> <value>jdbc:sqlserver://localhost:1433;DatabaseName=Organic </value> </property> <property name="username"> <value>sa</value> </property> <property name="password"> <value>123456</value> </property> </bean> <!-- 掃描存放SQL語句的Shop.xml --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="typeAliasesPackage" value="pojo"></property> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean> <!-- 掃描Mapper,并將其生命周期納入Spring的管理 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="mapper"></property> </bean> <!--4.配置事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--5.開啟注解進行事務管理 transaction-manager:引用上面定義的事務管理器--> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
springMVC配置文件 :
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 掃描Controller,并將其生命周期納入Spring管理 --> <context:component-scan base-package="controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 注解驅動,以使得訪問路徑與方法的匹配可以通過注解配置 --> <mvc:annotation-driven/> <!-- 靜態頁面,如html,css,js,images可以訪問 --> <mvc:default-servlet-handler /> <!-- 視圖定位 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
web.xml 配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>OrganicShopWithSSM</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- spring的配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring mvc核心:分發servlet --> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- spring mvc的配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!--配置由Spring 提供的針對中文亂碼的編碼過濾器 --> <!-- 編碼過濾器 --> <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> </web-app>
開始第一層啦:
pojo包:UserInfo 類
package pojo; public class UserInfo { private String uid; private String name; private String email; private String password; public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "UserInfo [uid=" + uid + ", name=" + name + ", email=" + email + ", password=" + password + "]"; } }
mapper層:(注意mybatis的xml文件也要放在mapper層)
ShopMapping.java:
其中@Param注解 是為了和xml中的查詢參數進行綁定
package mapper; import org.apache.ibatis.annotations.Param; import pojo.UserInfo; public interface ShopMapper { public void register(@Param("name")String name,@Param("email")String email,@Param("password")String password); public UserInfo login(@Param("email")String email,@Param("password")String password); public int findUser(@Param("email")String email); }
Shop.xml
<?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="mapper.ShopMapper"> <select id="login" resultType="UserInfo" parameterType="String" > select * from UserInfo where email=#{email} and password=#{password} </select> <select id="register" resultType="UserInfo"> insert into UserInfo(name,email,password) values (#{name},#{email},#{password}) </select> <select id="findUser" resultType="int"> select count(*) from UserInfo where email=#{email} </select> </mapper>
service層:其實在寫登陸的時候用了int類型,在想登陸也只要在數據庫中查詢表單輸入的數據就行了,在mapper層的xml的文件中也寫了 select count(*) 查詢個數, 但是結果并不好,因為我要做的還有設置session。
package service; import pojo.UserInfo; public interface ShopService { //用戶注冊 void regist(String name,String email,String password); //用戶登錄 UserInfo login(String email,String password); //驗證 int findUser(String email); }
service實現層:service.Impl
package service.Impl; import mapper.ShopMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import pojo.UserInfo; import service.ShopService; @Service public class ShopServiceImpl implements ShopService { @Autowired public ShopMapper sm; @Override public void regist(String name, String email, String password) { sm.register(name, email, password); } @Override public UserInfo login(String email, String password) { UserInfo user=sm.login(email, password); if(user!=null &&user.getPassword().equals(password)){ return user; } return null; } @Override public int findUser(String email) { if(sm.findUser(email)==0){ return 0; } return 1; } }
controller層:
package controller; import javax.servlet.http.HttpSession; 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.RequestMethod; import pojo.UserInfo; import service.ShopService; @Controller @RequestMapping("") public class ShopController { @Autowired public ShopService ss; @RequestMapping(value = "registerUser", method = RequestMethod.POST) public String registerUser(String name, String email, String password) { int findUser = ss.findUser(email); if (findUser == 0) { ss.regist(name, email, password); // System.out.println("可以注冊"); return "login"; } else { // System.out.println("注冊失敗"); return "register"; } } @RequestMapping(value = "loginUser", method = RequestMethod.POST) public String loginUser(UserInfo user, HttpSession session) { // 調用service方法 user = ss.login(user.getEmail(), user.getPassword()); if (user != null) { session.setAttribute("u".user); return "index"; } return "login"; } @RequestMapping("/outLogin") public String outLogin(HttpSession session){ session.invalidate(); return "index"; } }
在controller層當中,關于注冊的格式要求還需要自行搜索一下,主要講一下的是登陸。在登陸的這個方法中傳遞了兩個形式參數,UserInfo是實體類,HttpSssion是設置session的關鍵,后面通過session.setAttribute()設置session,這也是在上文中提到的需要session的部分。在后來的注銷中可以使用session.invalidate。
看完這篇關于SSM框架下如何實現登錄注冊的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。