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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址