• maven下配置ssm框架


    前提环境:maven环境已经搭建
    jdk版本:jdk1.8.0_60
    toncat版本:apache-tomcat-8.0.24
    maven版本:apache-maven-3.3.9
    ok,现在开始。
    1.建立maven web项目项目。
    在pom.xml添加依赖:

    <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/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.tccp</groupId>
        <artifactId>tccp</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <build>
            <plugins>
                <!-- define the project compile level -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
        <properties>
            <!-- spring版本号 -->
            <spring.version>4.1.6.RELEASE</spring.version>
            <!-- log4j日志文件管理包版本 -->
            <slf4j.version>1.6.6</slf4j.version>
            <log4j.version>1.2.12</log4j.version>
            <!-- junit版本号 -->
            <junit.version>4.10</junit.version>
            <!-- mybatis版本号 -->
            <mybatis.version>3.2.1</mybatis.version>
            <!-- aspectJ版本号 -->
            <aspectJ.version>1.8.9</aspectJ.version>
    
    
        </properties>
    
        <dependencies>
    
            <!-- 添加Spring依赖 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</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-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</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-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!-- 连接池 -->
            <dependency>
                <groupId>commons-dbcp</groupId>
                <artifactId>commons-dbcp</artifactId>
                <version>1.4</version>
            </dependency>
            <dependency>
                <groupId>commons-pool</groupId>
                <artifactId>commons-pool</artifactId>
                <version>1.6</version>
            </dependency>
            <!-- Json -->
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-mapper-asl</artifactId>
                <version>1.9.13</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.3</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.7.4</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.7.4</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.7.4</version>
            </dependency>
            <!--单元测试依赖 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
    
            <!-- 日志文件管理包 -->
            <!-- log start -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <!-- log end -->
    
            <!--spring单元测试依赖 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
                <scope>test</scope>
            </dependency>
    
            <!--mybatis依赖 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
    
            <!-- mybatis/spring包 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.2.0</version>
            </dependency>
    
            <!-- mysql驱动包 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.29</version>
            </dependency>
            <!-- AspectJ依赖 -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${aspectJ.version}</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>${aspectJ.version}</version>
            </dependency>
        </dependencies>
    
    </project>

    2.配置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"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">
        <display-name>ex</display-name>
    
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
        <!-- 日志记录 -->
            <context-param>  
            <param-name>log4jConfigLocation</param-name>  
            <param-value>/WEB-INF/log4j.properties</param-value>  
        </context-param>  
    
        <context-param>  
            <param-name>log4jRefreshInterval</param-name>  
            <param-value>60000</param-value>  
        </context-param>  
        <listener>  
            <listener-class>  
    org.springframework.web.util.Log4jConfigListener  
            </listener-class>  
        </listener>  
        <!-- 加载Spring容器配置 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!-- 设置Spring容器加载所有的配置文件的路径 此时便是加载spring/spring-application.xml文件-->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-*.xml</param-value>
        </context-param>
        <!-- 设计路径变量值 -->
        <context-param>
            <param-name>webAppRootKey</param-name>
            <param-value>springmvc.root</param-value>
        </context-param>
    
        <!-- Spring字符集过滤器 -->
        <filter>
            <filter-name>SpringEncodingFilter</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>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>SpringEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <!-- springMVC核心配置 -->
        <servlet>
            <servlet-name>dispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <!--spingMVC的配置路径  springMVC/spring-mvc.xml -->
                <param-value>classpath:springMVC/spring-mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <!-- 拦截设置 -->
        <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <!-- 错误跳转页面 -->
        <error-page>
            <!-- 路径不正确 -->
            <error-code>404</error-code>
            <location>/WEB-INF/errorpage/404.jsp</location>
        </error-page>
        <error-page>
            <!-- 没有访问权限,访问被禁止 -->
            <error-code>405</error-code>
            <location>/WEB-INF/errorpage/405.jsp</location>
        </error-page>
        <error-page>
            <!-- 内部错误 -->
            <error-code>500</error-code>
            <location>/WEB-INF/errorpage/500.jsp</location>
        </error-page>
    </web-app>

    3.配置spring的配置文件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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context" 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-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/aop    
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd" >
    
        <!-- 1、引入相关properties springProperties/*.properties-->
        <bean id="mappings"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations" value="classpath:springProperties/*.properties"></property>
        </bean>
    
        <!-- 2、以dbcp连接池方式配置数据源 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost:3306/tccp" />
            <property name="username" value="root" />
            <property name="password" value="123456" />
            <!-- 连接初始值,连接池启动时创建的连接数量的初始值 默认值是0 -->
            <property name="initialSize" value="0" />
            <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 默认值是0 -->
            <property name="minIdle" value="1" />
            <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 ,0时无限制 默认值是8 -->
            <property name="maxIdle" value="5" />
            <!-- 连接池的最大值,同一时间可以从池分配的最多连接数量,0时无限制 默认值是8 -->
            <property name="maxActive" value="15" />
        </bean>
    
        <!-- 3、 mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源,configLocation:配置文件位置 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
        </bean>
    
        <!-- 4、 mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory 
            basePackage:指定sql映射文件/接口所在的包(自动扫描) sqlSessionFactory:引用SQLSessionFactory -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.tc.tccp.web.dao"></property>
            <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
        </bean>
    
        <!-- 5、 事务管理 : DataSourceTransactionManager,dataSource:引用上面定义的数据源 -->
        <bean id="txManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    
        <!-- 6、 使用声明式事务 transaction-manager:引用上面定义的事务管理器 -->
        <tx:annotation-driven transaction-manager="txManager" />
    
    </beans>  
    

    database.properties:

    #连接池初始化配置
    ds.initialSize=1  
    ds.minIdle=1  
    ds.maxActive=20  
    
    #获取连接等待超时时间
    ds.maxWait=60000  
    
    #配置间隔多久进行空闲连接的检测,单位是毫秒
    ds.timeBetweenEvictionRunsMillis=60000  
    
    #配置一个连接池中线程的最小生存时间,单位是毫秒
    ds.minEvictableIdleTimeMillis=300000  

    jdbc.properties:

    jdbc.driverClassName=com.mysql.jdbc.Driver  
    jdbc.url=jdbc:mysql://localhost:3306/tccp  
    jdbc.username=root  
    jdbc.password=123456

    4.配置mybatis的配置文件mybatis-conf.xml

    <?xml version="1.0" encoding="UTF-8"?>    
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"    
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    
    <configuration>
        <settings>
            <!-- 这个配置使全局的映射器启用或禁用缓存 -->
            <setting name="cacheEnabled" value="true" />
            <!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->
            <setting name="multipleResultSetsEnabled" value="true" />
            <!-- 配置默认的执行器。SIMPLE 执行器没有什么特别之处。REUSE 执行器重用预处理语句。BATCH 执行器重用语句和批量更新 -->
            <setting name="defaultExecutorType" value="REUSE" />
            <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 -->
            <setting name="lazyLoadingEnabled" value="false" />
            <setting name="aggressiveLazyLoading" value="true" />
            <!-- <setting name="enhancementEnabled" value="true"/> -->
            <!-- 设置超时时间,它决定驱动等待一个数据库响应的时间。 -->
            <setting name="defaultStatementTimeout" value="25000" />
            <!--允许使用列标签代替列名 -->
            <setting name="useColumnLabel" value="true" />
            <!-- 不允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->
            <setting name="useGeneratedKeys" value="false" />
            <!-- 给予被嵌套的resultMap以字段-属性的映射支持 FULL,PARTIAL -->
            <setting name="autoMappingBehavior" value="PARTIAL" />
            <!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->
            <setting name="aggressiveLazyLoading" value="false" />
        </settings>
        <!-- 实体类,简称 -设置别名 方便mapper.xml映射文件中对类的书写,只需写别名,不再写带包的类 -->
        <typeAliases>
            <typeAlias alias="Test" type="com.tc.tccp.core.entity.Test" />
        </typeAliases>
    </configuration>  

    5.spring -mvc.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: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-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
        <!-- 注解扫描包 -->
        <context:component-scan base-package="com.tc.tccp" />
    
        <!-- 开启注解 -->
    
        <mvc:annotation-driven />
    
    
    
        <!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd -->
        <mvc:resources mapping="/images/**" location="/images/" />
        <mvc:resources mapping="/js/**" location="/js/" />
        <mvc:resources mapping="/css/**" location="/css/" />
        <mvc:resources mapping="/doc/**" location="/doc/" />
        <mvc:resources mapping="/html/**" location="/html/" />
    
        <!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
        <!-- 定义ResponseBody返回类型 -->
        <mvc:annotation-driven>
            <mvc:message-converters>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/plain;charset=UTF-8</value>
                            <value>text/html;charset=UTF-8</value>
                            <value>application/json; charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
                <bean
                    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>application/x-www-form-urlencoded; charset=UTF-8</value>
                            <value>application/json; charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>
        <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
        <!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
            默认编码 <property name="defaultEncoding" value="utf-8" /> 文件大小最大值 <property 
            name="maxUploadSize" value="10485760000" /> 内存中的最大值 <property name="maxInMemorySize" 
            value="40960" /> </bean> -->
    
    
    
    </beans>
    

    到此,配置文件就算是完了,
    然后就是,
    实体类:Use.java

    package com.tc.tccp.core.entity;
    
    public class User {
        private String id;
         private String username;
         private String password;
    
    
         public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getUsername() {
          return username;
         }
         public String getPassword() {
          return password;
         }
    
         public void setUsername(String username) {
          this.username = username;
         }
         public void setPassword(String password) {
          this.password = password;
         }
    
         public User(String username, String password) {
          super();
          this.username = username;
          this.password = password;
    
         }
         public User() {
          super();
         }
    
    
        }

    各层间:dao层:
    UserDao.java:

    package com.tc.tccp.web.dao;
    
    import java.util.List;
    import java.util.Map;
    
    import com.tc.tccp.core.entity.User;
    
    public interface UserDao {
    
        public boolean insert(User test);
        public User find(String id);
        public List<User> findAll();
        public boolean delete(String id);
        public boolean update(User us);
        public User findbyusernameandpassword(Map<String,String> map);
    }
    

    UserDao.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">
    <!-- 
        namespace:必须与对应的接口全类名一致
        id:必须与对应接口的某个对应的方法名一致
     -->
     <mapper namespace="com.tc.tccp.web.dao.UserDao">
    
        <insert id = "insert" parameterType = "User">
            insert into user(id, username,password) values(#{id}, #{username},#{password})
        </insert>
    
        <select id = "find" parameterType = "java.lang.String" resultType="User">
            select * from user where id = #{id}
        </select>
    
        <select id = "findAll" resultType="User">
            select * from user
        </select>
    
        <select id = "findbyusernameandpassword" resultType="User">
            select * from user where username=#{username} and password=#{password}
        </select>
    
    
        <delete id = "delete" parameterType = "java.lang.String">
            delete from user where id = #{id}
        </delete>
    
        <update id = "update" parameterType = "User" >
            update user set username = #{username},password=#{password}where id = #{id}
        </update>
     </mapper>

    service层:
    service接口UserService:

    package com.tc.tccp.web.service;
    
    import java.util.List;
    import java.util.Map;
    
    import com.tc.tccp.core.entity.User;
    
    
    public interface UserService {
        public User insert(User test);
        public User find(String id);
        public List<User> findAll();
        public boolean delete(String id);
        public User update(User test);
        public User findbyusernameandpassword(Map<String,String> map);
    }
    

    serviceimpl层实现类:UserServiceImpl:

    package com.tc.tccp.web.service.impl;
    
    import java.sql.SQLException;
    import java.util.List;
    import java.util.Map;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.tc.tccp.core.entity.User;
    import com.tc.tccp.core.exception.EntityException;
    import com.tc.tccp.web.dao.UserDao;
    import com.tc.tccp.web.service.UserService;
    
    @Service
    @Transactional
    public class UserServiceImpl implements UserService{
    
        String emp=null;
    
        @Autowired
        private UserDao userDao;
    
    
    
        @Override
        public User insert(User us) {
            try{
                 userDao.insert(us);
                 return userDao.find(us.getId());
            }catch(EntityException e){
                 throw new EntityException("查找用户错误",e);
            }
    
    
        }
    
        @Override
        public User find(String id) {
    
            User user = null;
            try {
                 user=userDao.find(id);
    
            }catch(EntityException e){
                 throw new EntityException("查找用户错误",e);
            }
    
            return user;
    
    
        }
    
        @Override
        public List<User> findAll() {
            try{
                 return userDao.findAll();
    
            }catch(EntityException e){
                 throw new EntityException("Error! when insert the entity",e);
            }
        }
    
        @Override
        public boolean delete(String id) {
            try{
                 return userDao.delete(id);
    
            }catch(EntityException e){
                 throw new EntityException("Error! when insert the entity",e);
            }
        }
    
        @Override
        public User update(User us) {
            try{
                 userDao.update(us);
                 return userDao.find(us.getId());
            }catch(EntityException e){
                 throw new EntityException("Error! when insert the entity",e);
            }
    
    
        }
    
        @Override
        public User findbyusernameandpassword(Map<String ,String > map){
            try{
             User us=userDao.findbyusernameandpassword(map);
             return userDao.find(us.getId());
             }catch(EntityException e){
                 throw new EntityException("Error! when insert the entity",e);
            }
        }
    
    }
    

    controller层:UserController:

    package com.tc.tccp.web.controller;
    
    import java.awt.Color;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.shiro.authc.AuthenticationException;
    import org.apache.shiro.authc.AuthenticationInfo;
    import org.apache.shiro.authc.AuthenticationToken;
    import org.apache.shiro.authc.SimpleAuthenticationInfo;
    import org.apache.shiro.authc.UsernamePasswordToken;
    import org.apache.shiro.authz.AuthorizationInfo;
    import org.apache.shiro.authz.SimpleAuthorizationInfo;
    import org.apache.shiro.realm.AuthorizingRealm;
    import org.apache.shiro.session.InvalidSessionException;
    import org.apache.shiro.session.Session;
    import org.apache.shiro.subject.PrincipalCollection;
    import org.apache.shiro.subject.Subject;
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.tc.tccp.core.entity.User;
    import com.tc.tccp.core.util.CryptographyUtil;
    import com.tc.tccp.core.util.ValidateCode;
    import com.tc.tccp.web.dao.UserDao;
    import com.tc.tccp.web.service.UserService;
    
    
    
    
    
    
    
    /** 
     * 自定义的指定Shiro验证用户登录的类 
     * @author abel.lin 
     */
    import javax.annotation.Resource;
    import javax.imageio.ImageIO;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import org.apache.log4j.Logger;
    import org.apache.shiro.SecurityUtils;
    import org.apache.shiro.authc.AuthenticationException;
    import org.apache.shiro.authc.UsernamePasswordToken;
    import org.apache.shiro.codec.Hex;
    import org.apache.shiro.subject.Subject;
    import org.apache.shiro.web.util.WebUtils;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.ui.ModelMap;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    @RequestMapping("/user")
    public class UserController {
    
        private static Logger logger = Logger.getLogger(UserController.class);
    
        @Resource
        private UserDao userDao;
    
        /**
         * 用户登录
         * 
         * @param user
         * @param result
         * @return
         */
        @RequestMapping(value = "/login")
        public String login( HttpServletRequest request) {
             String username = "wp";
            String password = "123";
            if (username == null || password == null) {
                System.out.println("登录失败:请检查您的用户名和密码!");
                return "/test/login";
            }
            Map<String, String> map = new HashMap<String, String>();
            map.put("username", username);
            map.put("password", password);
    
            User us = userDao.findbyusernameandpassword(map); // 根据用户名从数据库中查询出user信息
       if(us==null)
            return "/test/login";
            else return "test/zhuye";
    
    
        }
        //输入:http://localhost:8080/tccp/user/login即可访问
    
    }
    

    好了剩下的login.jsp页面自己不全
    项目目录:项目目录01
    项目路径02
    注意:项目路径中有shiro的配置信息及导入的类,文档中没加shiro,不要创建带shiro的路径。

  • 相关阅读:
    JAVA 正则表达式 (超详细)
    Windows命令-route
    Spring3 MVC请求参数获取的几种方法[转]
    Linux-支持中文
    Linux-Tmux使用初体验
    Linux命令-工作管理命令:&,ctrl+z,jobs,fg,bg
    Linux命令-服务管理命令:chkconfig
    Linux命令-文件系统常用命令:df,du,fsck,dumpe2fs
    Linux命令-用户管理:useradd,userdel,usermod,id,su,env,exit
    Linux命令-某个用户组下面的所有用户
  • 原文地址:https://www.cnblogs.com/wangxiaopei/p/8551271.html
Copyright © 2020-2023  润新知