ssm整合的登錄

新建一個web工程,主要結構如下:

ssm整合的登錄

數據庫創建如下:

ssm整合的登錄

控制層的代碼FormController 類

package codeRose.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

//動態頁面跳轉控制器
@Controller
public class FormController {
    @RequestMapping(value="/{formName}")
    public String loginForm(@PathVariable String formName){
        return formName;
    }

}

控制層UserController 主要注意view.setViewName("success");的跳轉。

package codeRose.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import codeRose.domain.User;
import codeRose.service.UserService;

@Controller
public class UserController {
    @Autowired
    @Qualifier("userService")
    private UserService userService;
    /**
     * 處理/login請求
     */
    @RequestMapping(value="/login")
    public ModelAndView login(String loginname,String password){
        User user=userService.login(loginname, password);
        ModelAndView view= new ModelAndView();
        if(user!=null){
            //登錄成功,將user對象設置到HttpSession作用范圍域中
            //session.setAttribute("user", user);
            //轉發到main請求
            //mv.setView(new RedirectView("/content/main.jsp"));
        	view.setViewName("success");
        }else {
            //登錄失敗,設置失敗信息,并調轉到登錄頁面
            
            //mv.addObject("message","登錄名和密碼錯誤,請重新輸入");
           // mv.setViewName("loginForm");
        	view.setViewName("login");
        }
        return view;
    }
}

dao層,實體類 User類

package codeRose.domain;

public class User {

	/**
	 * 實體層,存放實體類
	 */
	    private Integer id;
	    private String loginname;
	    private String password;
	    private String username;
		public Integer getId() {
			return id;
		}
		public void setId(Integer id) {
			this.id = id;
		}
		public String getLoginname() {
			return loginname;
		}
		public void setLoginname(String loginname) {
			this.loginname = loginname;
		}
		public String getPassword() {
			return password;
		}
		public void setPassword(String password) {
			this.password = password;
		}
		public String getUsername() {
			return username;
		}
		public void setUsername(String username) {
			this.username = username;
		}
		@Override
		public String toString() {
			return "User [id=" + id + ", loginname=" + loginname
					+ ", password=" + password + ", username=" + username + "]";
		}

}

持久層UserMapper

package codeRose.mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import codeRose.domain.User;
//持久層包括數據庫表映射的User.java的JavaBean對象,并使用了MyBatis的注解映射了對應的SQL語句

public interface UserMapper {

	@Select("select * from user where loginname=#{loginname} and password=#{password}")
    User findWithLoginAndPassword(@Param("loginname")String loginname,@Param("password")String password);

}

service層   UserService類 跟 UserServiceImpl 類

package codeRose.service;

import codeRose.domain.User;

public interface UserService {

	/**
     * 判斷用戶登錄
     * @param String loginname
     * @param String password
     * @return 找到返回User對象,沒有找到返回null
     * */
    User login(String loginname,String password);	
}
package codeRose.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import codeRose.domain.User;
import codeRose.mapper.UserMapper;
@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;

@Override
public User login(String loginname, String password) {

return userMapper.findWithLoginAndPassword(loginname, password);
}

} 

數據庫的db.properties

dataSource.driverClass=com.mysql.jdbc.Driver

dataSource.jdbcUrl=jdbc:mysql://localhost:3306/smm

dataSource.user=root

dataSource.password=

dataSource.maxPoolSize=20

dataSource.maxIdleTime = 1000

dataSource.minPoolSize=6

dataSource.initialPoolSize=5

主要的xml配置

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-4.2.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-4.2.xsd


http://www.springframework.org/schema/mvc


http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd


http://www.springframework.org/schema/tx


http://www.springframework.org/schema/tx/spring-tx-4.1.xsd


http://mybatis.org/schema/mybatis-spring

                        http://mybatis.org/schema/mybatis-spring.xsd ">
                  
     <!-- mybatis:scan會將codeRose.mapper包里的所有接口當作mapper配置,之后可以自動引入mapper類-->  
    <mybatis:scan base-package="codeRose.mapper"/>   
       
     <!-- 掃描codeRose包下面的java文件,有Spring的相關注解的類,則把這些類注冊為Spring的bean -->
    <context:component-scan base-package="codeRose"/>
    
    <!-- 使用PropertyOverrideConfigurer后處理器加載數據源參數 -->
    <context:property-override location="classpath:db.properties"/>

    <!-- 配置c3p0數據源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"/>
    
    <!-- 配置SqlSessionFactory,org.mybatis.spring.SqlSessionFactoryBean是Mybatis社區開發用于整合Spring的bean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
        p:dataSource-ref="dataSource"/>
    
    <!-- JDBC事務管理器 -->
    <bean id="transactionManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
         p:dataSource-ref="dataSource"/>
    
    <!-- 啟用支持annotation注解方式事務管理 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
</beans>

springmvc-config.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="

http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring-beans-4.2.xsd


http://www.springframework.org/schema/mvc


http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    <!-- 自動掃描該包,SpringMVC會將包下用了@controller注解的類注冊為Spring的controller -->
    <context:component-scan base-package="codeRose.controller"/>
    <!-- 設置默認配置方案 -->
    <mvc:annotation-driven/>
    <!-- 使用默認的Servlet來響應靜態文件 -->
    <mvc:default-servlet-handler/>
    <!-- 視圖解析器  -->
     <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <!-- 前綴 -->
         <!-- 前綴 -->
        <property name="prefix">
            <value>/WEB-INF/view/</value>
        </property>
        <!-- 后綴 -->
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    
</beans>

web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5"

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!– 配置spring核心監聽器,默認會以 /WEB-INF/applicationContext.xml作為配置文件 –>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!– contextConfigLocation參數用來指定Spring的配置文件 –>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/applicationContext.xml</param-value>

</context-param>

<!– 定義Spring MVC的前端控制器 –>

<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/springmvc-config.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<!– 讓Spring MVC的前端控制器攔截所有請求 –>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<!– 編碼過濾器 –>

<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>

主要的HTML頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="login" method="post">
        <label>賬號:</label>
        <input type="text" id="txtUsername" name="loginname" placeholder="請輸入賬號" /><br/>
        <label>密碼:</label>
        <input type="password" id="txtPassword" name="password" placeholder="請輸入密碼" /><br/>
        <input type="submit" value="提交" />
        <input type="reset" value="重置" />
    </form>
</body>
</html>

原文 

http://www.cnblogs.com/easyjie/p/11853601.html

本站部分文章源于互聯網,本著傳播知識、有益學習和研究的目的進行的轉載,為網友免費提供。如有著作權人或出版方提出異議,本站將立即刪除。如果您對文章轉載有任何疑問請告之我們,以便我們及時糾正。

PS:推薦一個微信公眾號: askHarries 或者qq群:474807195,里面會分享一些資深架構師錄制的視頻錄像:有Spring,MyBatis,Netty源碼分析,高并發、高性能、分布式、微服務架構的原理,JVM性能優化這些成為架構師必備的知識體系。還能領取免費的學習資源,目前受益良多

轉載請注明原文出處:Harries Blog? » ssm整合的登錄

贊 (0)
分享到:更多 ()

評論 0

  • 昵稱 (必填)
  • 郵箱 (必填)
  • 網址
2013平特肖公式