Spring+SpringMVC+Hiberate+MySql+Bootstrap实现一个简单验证登录实例

本例子结合Spring+SpringMVC+Hiberate+MySql+Bootstrap实现一个简单验证登录实例

1工程目录:

web.xml配置文件:Spring+SpringMVC+Hiberate+MySql+Bootstrap实现一个简单验证登录实例_第1张图片


<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/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Logindisplay-name>	
  <welcome-file-list>
    <welcome-file>index.jspwelcome-file>
  welcome-file-list>
  
  
  
  <context-param>
  		<param-name>contextConfigLocationparam-name>
  		<param-value>classpath*:config/spring/spring-*.xmlparam-value>
  context-param>
  
  
  <listener>
  		<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  
  
  <servlet>
  		<servlet-name>SpringMVCservlet-name>
  		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
  		<init-param>
  			<param-name>contextConfigLocationparam-name>
  			<param-value>classpath*:config/spring/spring-mvc.xmlparam-value>
  		init-param>
  		<load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
  		<servlet-name>SpringMVCservlet-name>
  		<url-pattern>/url-pattern>
  servlet-mapping>
  
  
  
   
  <filter>
  	<filter-name>encodingFilterfilter-name>
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
  	<init-param>
  		<param-name>encodingparam-name>
  		<param-value>UTF-8param-value>
  	init-param>
  	<init-param>
  		<param-name>forceEncodingparam-name>
  		<param-value>trueparam-value>
  	init-param>
  filter>
  <filter-mapping>
  	<filter-name>encodingFilterfilter-name>
  	<url-pattern>/*url-pattern>
  filter-mapping>
  
  
  
  <filter>
  	<filter-name>openSessionfilter-name>
  	<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilterfilter-class>
  filter>
  <filter-mapping>
  	<filter-name>openSessionfilter-name>
  	<url-pattern>/*url-pattern>
  filter-mapping>
  
  
web-app>

spring-mvc.xml配置文件:



<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:mvc="http://www.springframework.org/schema/mvc"
	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-3.2.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
	
	
	<context:component-scan base-package="com.web" />

	
	<mvc:annotation-driven />
	
	
	<mvc:resources location="/js/" mapping="/js/**"/>

		
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/">property>
		<property name="suffix" value=".jsp">property>
	bean>
beans>

spring-beans.xml配置文件:


<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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	
	<bean id="userDao" class="com.dao.UserDaoimpl">
		<property name="sessionFactory" ref="sessionFactory">property>
	bean>
	<bean id="userManagerBase" class="com.manager.ManagerImpl">
		<property name="userDao" ref="userDao">property>
	bean>
	<bean name="manager" parent="transactionProxy">
		<property name="target" ref="userManagerBase">property>
	bean>	
	
	
	
	beans>

spring-common.xml配置文件:


<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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
		<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
		<property name="url" value="jdbc:mysql://localhost/test">property>
		<property name="username" value="root">property>
		<property name="password" value="1996112lin">property>
	bean>
	
	
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>
				<prop key="hibernate.hbm2ddl.auto">updateprop>
				<prop key="hibernate.show_sql">trueprop>
				<prop key="hibernate.format_sql">trueprop>
			props>
		property>
		<property name="annotatedClasses">
			<list>
				<value>com.eneity.Uservalue>
			list>
		property>
	bean>
	
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
	bean>
	
	
	<bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">  
	    <property name="transactionManager" ref="transactionManager">property>  
	    <property name="transactionAttributes">  
	        <props>  
	            <prop key="add*">PROPAGATION_REQUIRED,-Exceptionprop>  
	            <prop key="modify*">PROPAGATION_REQUIRED,-myExceptionprop>  
	            <prop key="del*">PROPAGATION_REQUIREDprop>  
	            <prop key="*">PROPAGATION_REQUIREDprop>  
	        props>  
	    property>  
	bean> 
	
	
	beans>

采用注解实现实体类文件User.java:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;




@Entity
@Table(name="myuser")
public class User {
	@Id
	@GeneratedValue(generator="system-uuid")
	@GenericGenerator(name = "system-uuid",strategy="uuid")
	@Column(length=32)
	
	private String id;
	
	
	@Column(length=32)
	private String name;
	
	@Column(length=32)
	private String password;

	public String getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	
	
	
}

实体类操作接口UseDao.java:

public interface UserDao {
		public String check(String name,String password);
}

具体实现类UserDaoimpl.java:

import java.util.List;

import javax.management.Query;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class UserDaoimpl implements UserDao{
	
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}




	@Override
	public String check(String name, String password) {
		String hql = "select user.name ,user.password from User as user where user.name='"+name+"' and user.password ='"+password+"'";
		
		org.hibernate.Query query = sessionFactory.getCurrentSession().createQuery(hql);
		
		
		List list = query.list(); 
		 if (list.size() > 0) {
			return "ok";
		}
		
		
		return "no";
	}

}

管理接口Manager.java:

public interface Manager {
	public String check(String name,String password); 
		
	
}

管理实现类ManagerImpl.java:

import com.dao.UserDao;

public class ManagerImpl implements Manager{
	private UserDao userDao;
	
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public String check(String name,String password) {
		return userDao.check(name, password);
	}
	
	
	
}

控制类Controler.java:

import javax.annotation.Resource;
import javax.enterprise.inject.Model;

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

import com.dao.UserDao;
import com.eneity.User;
import com.manager.Manager;



@Controller
@RequestMapping("/login")
public class Controler {
		@Resource
		private Manager manager;
		
		public void setManager(Manager manager) {
			this.manager = manager;
		}

		@RequestMapping("/check")
		public String check(User user,org.springframework.ui.Model model) {
			String string = manager.check(user.getName(), user.getPassword());
			if (string.equals("ok")) {
				model.addAttribute("name",user.getName());
				return "/ok";
			}
			return "/no";
		}
	
}

登录主页面index.jsp(加入了BootStrap实现效果):

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    
	
	
	    
	
	
	 用户登录
        
        
        
        
         
  
  
  
    
    		  

请登录

记住密码

ok.jsp和no.jsp就不给出了,最终实现效果:Spring+SpringMVC+Hiberate+MySql+Bootstrap实现一个简单验证登录实例_第2张图片

你可能感兴趣的:(Spring学习)