[工作流与Activiti] - No.2 简易工作流系统搭建(1) : 用户管理


前一段时间一直在实训,给实训单位做了一些关于工作流引擎的项目。由于老师说这部分东西不属于保密部分,所以在这里记录一下自己学习activiti的一些心得。这个系列的文章将分为四部分,用户管理、模型管理、流程管理和Activiti modeler整合四个部分。在本篇文章中,我们讲解最简单的部分,那就是系统的用户管理

一、创建Maven项目:

1、创建maven webapp类型项目









2、设置项目文件夹:

在main包下右键java新建文件夹。然后在File –Project Structure中,设置文件夹类型:
Java包:源代码
Resource包:资源文件夹
Webapp:web文件夹










最后项目文件夹目录如下图所示:



根据上述结构图,创建文件夹及相关配置文件

注:加红线部分可以不创建,在后续整合activiti modeler会用到

二、配置SpringMVC

a) 配置webapp/WEB-INF目录下的web.xml
为web.xml添加以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>web.root</param-value>
</context-param>
<!--配置springmvc DispatcherServlet-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--Sources标注的文件夹下需要新建一个spring文件夹-->
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/applicationContext.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.woff</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.svg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.map</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ttf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.json</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.xml</url-pattern>
</servlet-mapping>
<filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
b) applicationContext.xml中配置SpringMVC Servlet相关

<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" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.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.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--启用spring的一些annotation -->
<context:annotation-config/>

<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制\器 -->
<context:component-scan base-package="com.adc.gim.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven/>
<!-- 使用thymeleaf解析 -->
<bean id="templateResolver"
class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML" />
<property name="cacheable" value="false" />
</bean>

<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>

<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8"/>
</bean>
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- dataSourse连接池相关属性,代码不在此贴出,会放在打包好的项目里面 -->
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<context:component-scan base-package="com.adc.gim.service" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<context:component-scan base-package="com.adc.gim.repository" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- 支持注解方式声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
<jpa:repositories base-package="com.adc.gim.repository" repository-impl-postfix="Impl" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager" />

<!-- 实体管理器 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.adc.gim.entity" />
<property name="persistenceProvider">
<bean class="org.hibernate.ejb.HibernatePersistence" />
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="database" value="MYSQL" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
<!-- <property name="showSql" value="true" /> -->
</bean>
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.query.substitutions" value="true 1, false 0" />
<entry key="hibernate.default_batch_fetch_size" value="16" />
<entry key="hibernate.max_fetch_depth" value="2" />
<entry key="hibernate.generate_statistics" value="true" />
<entry key="hibernate.bytecode.use_reflection_optimizer" value="true" />
<entry key="hibernate.cache.use_second_level_cache" value="false" />
<entry key="hibernate.cache.use_query_cache" value="false" />
<entry key="hibernate.hbm2ddl.auto" value="update"/>
</map>
</property>
</bean>

<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />

<property name="validationQuery" value="SELECT 'x' from dual" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--spring session -->
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="127.0.0.1"/>
<property name="port" value="6379"/>
<property name="database" value="0"/>
</bean>
<!--- ************************************************* -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.adc.gim.service.impl.*.*(..))"/>
</aop:config>
</beans>

c)配置数据库jdbc.properties,注意更改你的数据库链接、用户和密码

jdbc.url=jdbc:mysql://localhost:3306/activitimvc?characterEncoding=UTF-8&characterSetResults=UTF-8
jdbc.username=root
jdbc.password=root
jdbc.driverClassName=com.mysql.jdbc.Driver

#hibernate config
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = update
d) 在pom 中添加以下依赖:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.adc.gim</groupId>
<artifactId>activiti3.0</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>activiti3.0 Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>jboss-maven2-release-repository</id>
<url>https://repository.jboss.org/nexus/content/repositories/thirdparty-releases/</url>
</repository>
</repositories>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.6.RELEASE</spring.version>
<activiti.version>5.18.0</activiti.version>
<hibernate.version>5.1.0.Final</hibernate.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- Spring Data Redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.4.RELEASE</version>
</dependency>
<!-- Spring Session -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
<!-- Apache Commons Pool -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>



<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->

<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>


<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>

<!--j2ee相关包 servlet、jsp、jstl-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.11.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>activiti4.0</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
三、增加实体类、控制类和工具类

1. 增加用于用户管理的用户实体类

User.java

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

/**
* Created by YanMing on 2017/7/13.
*/
@Entity
@Table(name = "t_user")
public class User implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(updatable = false, nullable = false)
private Long id;

@Column(name = "username", nullable = false)
private String username;

@Column(name = "password", nullable = false)
private String password;


public User(String username, String password) {
this.username = username;
this.password = password;
}
public User(){

}

public Long getId() {
return id;
}

public void setId(Long 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;
}


}
2、添加控制类

LoginController.java到controller包

package com.adc.gim.controller;

import com.adc.gim.entity.User;
import com.adc.gim.service.UserService;
import com.adc.gim.utils.SpringSessionUtil;
import com.sun.org.apache.bcel.internal.generic.AALOAD;
import org.activiti.engine.IdentityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;

/**
* @Date: 13:41 2017/6/5
* 控制登陆、注册、主页跳转功能
*
*/
@Controller
@RequestMapping("/")
public class LoginController {


    /**
     * @Date: 13:18 2017/6/6
     * 控制"/login"、"/"跳转,检测session中是否登录
     * 如果登录直接跳转达到主页index.html
     */
    @Autowired
    private UserService userService;




    /*@Autowired
    private IdentityService identityService;
    */
    @RequestMapping(value = {"/login","/"}, method = RequestMethod.GET)
    public String login(HttpSession session) {
        User tmpUser = SpringSessionUtil.getSession(session);
        if (tmpUser != null) {
            return "redirect:/loginOK";
        } else {
            return "loginPage";
        }


    }
    /**
     * @Date: 13:21 2017/6/6
     * 跳转到注册页面
     */
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String register() {


        return "registerPage";
    }




    /*@RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public String registerPost(@RequestParam("username") String username,
                               @RequestParam("password") String password,
                               @RequestParam("group") String group) {


        User user = userService.getUserByUsername(username);
        if (user == null){
            User newUser = new User(username, password);
            userService.saveNewUser(newUser);
            org.activiti.engine.identity.User act_user = identityService.newUser(username);
            act_user.setId(username);
            act_user.setFirstName(username);
            act_user.setPassword(password);


            identityService.saveUser(act_user);
            identityService.createMembership(username, group);
            return "register_success";
        }


        return "user_has_existed";
    }
*/


    /**
     * @Date: 13:22 2017/6/6
     * 登录功能返回登录状态码
     * right/wrong
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public String userLogin(@RequestParam(value = "username") String username,
                            @RequestParam(value = "password") String password,
                            HttpSession session) {
        //检验是否session中是否已登录
        User tmpUser = userService.getUserByUsername(username);
        if (tmpUser != null && (tmpUser.getPassword().equals(password))) {
            //登录当前用户,保存到session
            SpringSessionUtil.setSession(session, tmpUser);
            return "right";
        } else {
            return "wrong";
        }
    }


    /**
     * @Date: 13:27 2017/6/6
     * 登陆成功跳转
     */
    @RequestMapping(value = "/loginOK")
    public String loginOK() {
        //return "mainPage";
        return "index";
    }


    /**
     * @Date: 13:28 2017/6/26
     * 登陆退出
     */
    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
        session.removeAttribute(SpringSessionUtil.USER);
        return "loginPage";
    }


}

3、添加服务类,把UserService接口和UserServiceImpl添加到service和service.impl中

UserService.java

package com.adc.gim.service;

import com.adc.gim.entity.User;

/**
* Created by YanMing on 2017/6/6.
*/

/**
* @Autohr:YanMing
* @Description:
* @Date: 13:40 2017/6/15
*/
public interface UserService {

User getUserById(Long id);
User getUserByUsername(String username);
void saveNewUser(User user);
}
UserServiceImpl.java

package com.adc.gim.service.impl;

import com.adc.gim.entity.User;
import com.adc.gim.repository.UserRepository;
import com.adc.gim.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* Created by YanMing on 2017/6/6.
*/
@Service
@ComponentScan(basePackages = "com.adc.gim.repository")
public class UserServiceImpl implements UserService {

@Autowired
private UserRepository userRepository;

@Autowired
private JdbcTemplate jdbcTemplate;

/**
* @Date: 16:56 2017/6/2
* 根据id获取用户
*/
public User getUserById(Long id) {
return userRepository.findById(id);
}

/**
* @Date: 16:56 2017/6/2
* 根据用户名获取用户
*/
public User getUserByUsername(String username) {
return userRepository.findByUsername(username);
}

public void saveNewUser(User user){
String sql4user = "insert into t_user(username,password)VALUES (?,?)";
//String sql4group = "insert into t_user(username,password)VALUES (?,?)"
//KeyHolder resKeyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(sql4user, new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1,user.getUsername());
ps.setString(2,user.getPassword());
}
});
//String uid = String.valueOf(resKeyHolder.getKey().intValue());

}

}

4、添加数据库访问类

UserRepository.java

package com.adc.gim.repository;

import com.adc.gim.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* Created by YanMing on 2017/6/6.
*/
/**
* @Date: 16:13 2017/6/18
* 实现jpaRepository<>接口
* 访问jpa
*/
@Repository
@Transactional
public interface UserRepository extends JpaRepository<User,Long> {

User findByUsername(String username);
User findById(Long id);
}

5、添加通用工具,把SpringSessionUtil添加到util包中


package com.adc.gim.utils;

import com.adc.gim.entity.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
* Created by YanMing on 2017/6/28.
*/
public class SpringSessionUtil {

public static final String USER = "user";

public static void setSession(HttpSession session, User user)
{
session.setAttribute(USER, user);
}

public static User getSession(HttpSession session) {
Object attribute = session.getAttribute(USER);
return attribute == null ? null : (User) attribute;
}
}
6、在WEB-INF下的view中添加loginPage.html和index.html

loginPage.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<link rel="shortcut icon" href="/static/images/favicon.ico" />
<link rel="bookmark" href="/static/images/favicon.ico" />
<title>登录</title>
<link href="/static/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<link href="/static/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
<link href="/static/css/sweetalert.min.css" rel="stylesheet" type="text/css"/>
<link href="/static/css/semantic.min.css" rel="stylesheet" type="text/css"/>
<style>
body{
background: url("static/images/background.jpg") no-repeat center/cover;
padding-top: 200px;
}
</style>
</head>
<body>

<h2 style="text-align: center;">用户登录</h2>
<br/><br/>
<div class="container" style="width: 600px">
<div class="input-group">
<div class="input-group-addon"><span class="glyphicon glyphicon-user"></span> </div>
<input type="text" id="username" name="username" class="form-control" placeholder="用户名"/>

</div>
<br/>
<div class="input-group">
<div class="input-group-addon"><span class="glyphicon glyphicon-lock"></span> </div>
<input type="password" id="password" name="password" class="form-control" placeholder="密 码"/>
</div>
<br/>
<input type="button" class="form-control btn btn-success" onclick="clickLogin()" value="登 陆"/>

<br/><br/>
<p style="text-align: right">还没注册?<a href="/register">快去注册</a></p>

</div>

<script src="/static/js/jquery.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
<script src="/static/js/sweetalert.min.js"></script>
<script src="/static/js/semantic.min.js"></script>
<script src="/static/js/moment.min.js"></script>
<script src="/static/js/loginCtrl.js"></script>
</body>
</html>

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
<meta charset="UTF-8"/>
<link rel="shortcut icon" href="/static/images/favicon.ico"/>
<link rel="bookmark" href="/static/images/favicon.ico"/>
<title th:text="${session.user.username} + ' | 管理'"></title>
<link href="/static/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<link href="/static/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
<link href="/static/css/sweetalert.min.css" rel="stylesheet" type="text/css"/>
<link href="/static/css/semantic.min.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top"
style="background-color: #f6f6f6; border: none;box-shadow: 0px 2px 8px 0px rgba(50,50,50,0.25);" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="">
<img alt="网站Logo" src="/static/images/logo.png" style="margin-top: -10px; width: 35px"/>
</a>
<a class="navbar-brand" href="/">
系统管理
</a>
</div>


<ul class="nav navbar-nav">
<li class="active"><a href="/"><span class="glyphicon glyphicon-pencil"></span>  当前任务</a></li>
<li><a href="/modelPage"><span class="glyphicon glyphicon-eye-open"></span>  模型管理</a></li>
<li><a href="/processPage"><span class="glyphicon glyphicon-briefcase"></span>  流程管理</a></li>
</ul>


<div class="ui inline dropdown navbar-right" style="line-height: 50px">
<div class="text">
<img class="ui avatar image" src="/static/images/avatar/default.png"/>
<span id="username" th:text="${session.user.username}"></span>
</div>
<i class="dropdown icon"></i>
<div class="menu">
<div class="item" style="color: black" id="logout"><a href="/logout"><span
class="glyphicon glyphicon-log-out"></span>  退出登录</a></div>
</div>
</div>
</div>
</nav>

<div class="container-fluid" style="margin-top: 70px">
<div class="row" style="">
<div class="col-xs-2">
<ul class="nav nav-pills nav-stacked">
<li class="active" data-toggle="tab" href="#my_task"><a href="">我的待办 </a></li>
<li><a data-toggle="tab" href="#history_record">历史记录 </a></li>
</ul>
</div>

<div class="col-xs-10 tab-content">
<div class="tab-pane fade active in" id="my_task">
<div class="panel panel-default">
<!--<div class="panel-heading"><h3>我的待办</h3></div>-->
<div class="panel-body">
<ol class="breadcrumb">
<li><a href="/">当前任务</a></li>
<li class="active">我的待办</li>
</ol>
<div id="myAssignment">
<table class="table table-bordered table-striped">
<thead>
<tr class="info">
<th style="text-align: center">任务ID</th>
<th style="text-align: center">任务名称</th>
<th style="text-align: center">开始时间</th>
<th style="text-align: center">申请人</th>
<th style="text-align: center">操作</th>
</tr>
</thead>
<tbody id="ma_tbid" style="text-align: center">

</tbody>
</table>
</div>
<div id="cmntDiv">
<div class="modal fade" id="completeModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">任务办理</h4>
</div>
<div class="modal-body" id="modal-body">

</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
<button type="button" class="btn btn-success" data-dismiss="modal" onclick="completeTask()">确认办理</button>
</div>
</div>
</div>
</div>
</div>
<br/><br/>


<div class="modal fade" id="processImageModal" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel">
<div style="width: 1000px; height: 600px" class="modal-dialog model-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myProcessImageModalLabel">   >   当前进度</h4>
</div>
<div class="modal-body" id="process-image-modal-body">

</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关 闭</button>

</div>
</div>
</div>
</div>


<div id="actiDiv" style="display: none;">
<table class="table table-bordered table-striped">
<thead>
<tr class="info">
<th style="text-align: center">Id</th>
<th style="text-align: center">活动Id</th>
<th style="text-align: center">活动名称</th>
<th style="text-align: center">活动类型</th>
<th style="text-align: center">开始时间</th>
<th style="text-align: center">结束时间</th>
<th style="text-align: center">处理人</th>
<th style="text-align: center">查看详情</th>
</tr>
</thead>
<tbody id="ac_tbid">

</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="tab-pane fade" id="history_record" style="padding-left: 10px;">
<div class="panel panel-default">
<div class="panel-body">
<ol class="breadcrumb">
<li><a href="/">当前任务</a></li>
<li class="active">历史记录</li>
</ol>
<div id="HisDiv">
<table class="table table-bordered table-striped">
<thead>
<tr class="info">
<th style="text-align: center">流程ID</th>
<th style="text-align: center">流程名称</th>
<th style="text-align: center">开始时间</th>
<th style="text-align: center">流程状态</th>
<th style="text-align: center">操作</th>
</tr>
</thead>
<tbody id="his_tbid" style="text-align: center">

</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="modal fade" id="hisDtlModal" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="task-detail-modal">办理细节</h4>
</div>
<div id="hisDtlDiv" class="modal-body">
<table class="table table-bordered table-striped">
<thead>
<tr style="text-align: center">
<th style="text-align: center">表单字段名</th>
<th style="text-align: center">表单属性值</th>
</tr>
</thead>
<tbody id="hisdtl_tbid">

</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">关 闭</button>
</div>
</div>
</div>
</div>

<div class="modal fade" id="historyProcessImageModal" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel">
<div style="width: 1000px; height: 600px" class="modal-dialog model-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="historyProcessImageModalLabel">历史记录   >   当前进度</h4>
</div>
<div class="modal-body" id="history-process-image-modal-body">

</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关 闭</button>

</div>
</div>
</div>
</div>
</div>
</div>
</div>




</body>
<script src="/static/js/jquery.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
<script src="/static/js/sweetalert.min.js"></script>
<script src="/static/js/semantic.min.js"></script>
<script src="/static/js/moment.min.js"></script>
<script src="/static/js/indxCtrl.js"></script>
</html>
注:两个html页面所用到的所有资源文件以及js代码均不在这里展示,源码会上传到github上

7、配置tomcat

不同ide配置方式不同,配置方法也比较简单,相信大家都会这个配置tomcat的过程,不再赘述

8、导入数据库

由于用户注册部分设计在Activiti数据库中插入相关的数据,所以在这篇文章不涉及用户注册的过程。我们提前在数据库中插入一条数据:


9、启动Tomcat,使用刚刚插入的用户信息登录,运行结果如下:

登录界面:


注册界面:


登录成功界面:


P.S. 文章不妥之处还望指正


本文Github源码地址














智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告