在IntelliJ IDEA上搭建Spring+SpringMVC+Mybatis+Shiro环境搭建(一)
来源:互联网 发布:天猫属于淘宝吗 编辑:程序博客网 时间:2024/05/07 10:02
IntelliJ IDEA 下载地址:http://www.jetbrains.com/idea/ 都是最新版本的,只要破解一个版本,其他版本可以共用,具体参考百度O(∩_∩)O
Maven Reposity Maven仓库地址:http://mvnrepository.com/
在IDEA上新建一个Project ,然后选择Maven,最后选择webapp
图1.1
第二页填maven项目的信息
图1.2
图1.3
工程建立完成之后,要注意文件结构
图1.4
工程目录建立完成之后,要建立工程的环境设置
图1.5
在modules中设置web.xml路径
图1.6
下面去pom.xml中添加jar包
<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"> <!--Maven坐标--> <modelVersion>4.0.0</modelVersion> <groupId>com.alec</groupId> <artifactId>pm</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>pm Maven Webapp</name> <!--参数--> <properties> <project.bulid.sourceEncoding>UTF-8</project.bulid.sourceEncoding> <springframework.version>4.2.4.RELEASE</springframework.version> </properties> <url>http://maven.apache.org</url> <!--依赖jar包--> <dependencies> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--servlet 的api--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!--servlet 的jstl--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--springMvc的jar包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframework.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${springframework.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <!--Mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <!--JSR验证框架--> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.2.2.Final</version> </dependency> <!--mybatis和Spring整合--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.3</version> </dependency> <!--pageHelper分页--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>3.3.2</version> </dependency> <!--数据库连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.16</version> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.25</version> <scope>runtime</scope> </dependency> <!--shiro--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-quartz</artifactId> <version>1.2.4</version> </dependency> <!--mongodb--> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.2.0</version> </dependency> <!--log4j和slf4整合--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> </dependencies> <build> <resources> <!--资源文件路径--> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </resource> </resources> <plugins> <plugin> <!--给maven指定java编译环境--> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>${project.bulid.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <!--mybatis反向生成工具--> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> </dependencies> </plugin> </plugins> </build></project>
这是pom里面需要导入的jar和各种信息
配置Tomcat
图1,7
图1.8
剩下的事,就是很常规的做SSM的整合,可以参考 http://blog.csdn.net/u011403655/article/details/46843331
做完SSM整合之后需要进一步整合Shiro
Shiro整合一,在pom..xml文件中加入shiro-core,shiro-spring,shiro-web,shiro-ehcach,shiro-quartz的依赖
Shiro整合二,建立spring-shiro.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: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-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- 加载配置文件 --> <context:property-placeholder location="classpath*:config.properties"/> <!-- Shiro权限过滤URL定义 --> <bean name="shiroFilterChainDefinitions" class="java.lang.String"> <constructor-arg> <value> /static/** = anon /login = authc /logout = logout /** = user </value> </constructor-arg> </bean> <!-- 安全认证过滤器 --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager" /> <property name="loginUrl" value="/login" /> <property name="successUrl" value="/index" /> <property name="filters"> <map> <entry key="authc" value-ref="formAuthFilter"/> </map> </property> <property name="filterChainDefinitions"> <ref bean="shiroFilterChainDefinitions"/> </property> </bean> <!-- 定义Shiro安全管理配置 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="userPassAuthRealm" /> <property name="sessionManager" ref="sessionManager" /> <property name="cacheManager" ref="shiroCacheManager" /> </bean> <!-- 自定义会话管理配置 --> <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> <property name="sessionDAO" ref="sessionDAO"/> <!-- 会话超时时间,单位:毫秒 --> <property name="globalSessionTimeout" value="180000"/> <!-- 定时清理失效会话, 清理用户直接关闭浏览器造成的孤立会话 --> <property name="sessionValidationInterval" value="120000"/> <property name="sessionValidationSchedulerEnabled" value="true"/> <property name="sessionIdCookie" ref="sessionIdCookie"/> <property name="sessionIdCookieEnabled" value="true"/> <property name="deleteInvalidSessions" value="true"/> <property name="sessionValidationScheduler" ref="sessionValidationScheduler"/> </bean> <!-- 会话验证调度器 --> <bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.quartz.QuartzSessionValidationScheduler"> <property name="sessionValidationInterval" value="1800000"/> <property name="sessionManager" ref="sessionManager"/> </bean> <!-- 指定本系统SESSIONID, 默认为: JSESSIONID 问题: 与SERVLET容器名冲突, 如JETTY, TOMCAT 等默认JSESSIONID, 当跳出SHIRO SERVLET时如ERROR-PAGE容器会为JSESSIONID重新分配值导致登录会话丢失! --> <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie"> <constructor-arg name="name" value="alec.session.id"/> </bean> <!-- 会话ID生成器 --> <bean id="sessionIdGenerator" class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator"/> <!-- Session存储容器 --> <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO"> <property name="sessionIdGenerator" ref="sessionIdGenerator" /> <property name="cacheManager" ref="shiroCacheManager" /> </bean> <!-- 定义授权缓存管理器 --> <bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManager" ref="ehCacheManager"/> </bean> <!-- 保证实现了Shiro内部lifecycle函数的bean执行 --> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/></beans>
Shiro整合三,在web.xml中配置
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath:spring-beans.xml,
- classpath:spring-shiro-web.xml
- </param-value>
- </context-param>
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <filter>
- <filter-name>shiroFilter</filter-name>
- <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
- <init-param>
- <param-name>targetFilterLifecycle</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>shiroFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
Shiro整合三,EhCache配置
EhCache有两种配法,
第一种
在spring-shiro.xml的文件里直接配置
- <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
- <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
- </bean>
在spring-shiro.xml的文件里
<!-- 定义授权缓存管理器 --><bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManager" ref="ehCacheManager"/></bean>然后在spring-core.xml或者spring-mybatis.xml中配置 ehCacheManager
<!-- 缓存管理器 使用Ehcache实现 --><bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:cache/ehcache.xml"/></bean>
配置完成!
可以写一个Controller来测试一下
@Controllerpublic class LoginController { @RequestMapping("/login") public String login() { return "sys/login"; }}
前台写一个JSP,login,jsp
<%@page pageEncoding="utf-8" language="java" %><%@ include file="/WEB-INF/views/include/taglib.jsp"%><!DOCTYPE html><html><head> <title>权限管理系统 登录</title> <%@ include file="/WEB-INF/views/include/head.jsp"%> <style type="text/css"> html,body,table{background-color:#f5f5f5;width:100%;text-align:center;} .form-signin-heading{font-family:Helvetica, Georgia, Arial, sans-serif, 黑体;font-size:36px;margin-bottom:20px;color:#0663a2;} .form-signin{position:relative;text-align:left;width:300px;padding:25px 29px 29px;margin:0 auto 20px;background-color:#fff;border:1px solid #e5e5e5; -webkit-border-radius:5px;-moz-border-radius:5px;border-radius:5px;-webkit-box-shadow:0 1px 2px rgba(0,0,0,.05);-moz-box-shadow:0 1px 2px rgba(0,0,0,.05);box-shadow:0 1px 2px rgba(0,0,0,.05);} .form-signin .checkbox{margin-bottom:10px;color:#0663a2;} .form-signin .input-label{font-size:16px;line-height:23px;color:#999;} .form-signin .input-block-level{font-size:16px;height:auto;margin-bottom:15px;padding:7px;*width:283px;*padding-bottom:0;_padding:7px 7px 9px 7px;} .form-signin .btn.btn-large{font-size:16px;} .form-signin #themeSwitch{position:absolute;right:15px;bottom:10px;} .form-signin div.validateCode {padding-bottom:15px;} .mid{vertical-align:middle;} .header{height:80px;padding-top:20px;} .alert{position:relative;width:300px;margin:0 auto;*padding-bottom:0px;} label.error{background:none;width:270px;font-weight:normal;color:inherit;margin:0;} </style> <script type="text/javascript"> $(document).ready(function() { $("#loginForm").validate({ messages: { username: {required: "请填写用户名."},password: {required: "请填写密码."} }, errorLabelContainer: "#messageBox", errorPlacement: function(error, element) { error.appendTo($("#loginError").parent()); } }); }); // 如果在框架或在对话框中,则弹出提示并跳转到首页 if(self.frameElement && self.frameElement.tagName == "IFRAME" || $('#left').length > 0 || $('.jbox').length > 0){ alert('未登录或登录超时。请重新登录,谢谢!'); top.location = ""; } </script></head><body> <!--[if lte IE 6]> <br/> <div class='alert alert-block' style="text-align:left;padding-bottom:10px;"> <a class="close" data-dismiss="alert">x</a> <h4>温馨提示:</h4> <p>你使用的浏览器版本过低。为了获得更好的浏览体验,强烈建议您 <a href="http://browsehappy.com" target="_blank">升级</a> 到最新版本的IE浏览器,或者使用较新版本的 Chrome、Firefox、Safari 等。</p> </div> <![endif]--> <div class="header"> <div id="messageBox" class="alert alert-error hide"> <button data-dismiss="alert" class="close">×</button> <label id="loginError" class="error"></label> </div> </div> <h1 class="form-signin-heading">权限管理系统</h1> <form id="loginForm" class="form-signin" action="index.html" method="post"> <label class="input-label" for="username">登录名</label> <input type="text" id="username" name="username" class="input-block-level required" value=""> <label class="input-label" for="password">密码</label> <input type="password" id="password" name="password" class="input-block-level required"> <input class="btn btn-large btn-primary" type="submit" value="登 录"> <label for="rememberMe" title="下次不需要再登录"><input type="checkbox" id="rememberMe" name="rememberMe"> 记住我</label> </form> <div class="footer"> Copyright © 2015-2015 <a href="http://www.tanzhouedu.com/" target="_blank">权限管理系统</a> - Powered By Colin V0.0.1 </div> <script src="./权限管理系统 登录_files/zoom.min.js" type="text/javascript"></script><div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="http://localhost:8080/login;JSESSIONID=8c063142-ef1d-4b1d-ab15-fb0ecf1e6488#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div><br style="clear: both;"></div><div id="haloword-content"></div></div></body></html>
若访问所有页面都是调到login.jsp 则证明配置成功.
- 在IntelliJ IDEA上搭建Spring+SpringMVC+Mybatis+Shiro环境搭建(一)
- 使用intellij idea搭建spring-springmvc-mybatis整合框架环境
- springMVC+maven+mybatis+Intellij IDEA环境搭建
- springMVC+maven+mybatis+Intellij IDEA环境搭建
- springMVC+maven+mybatis+Intellij IDEA环境搭建
- Springmvc + Mybatis + IntelliJ IDEA 环境搭建
- springMVC+maven+mybatis+Intellij IDEA环境搭建
- 搭建springmvc+spring+mybatis+IntelliJ IDEA例子
- IntelliJ IDEA spring mvc +mybatis 环境搭建服务器(上)
- Idea+Maven+Spring+SpringMVC+MyBatis环境搭建
- 使用intellij idea搭建MAVEN+SSM(Spring+SpringMVC+MyBatis)框架
- 使用intellij idea搭建MAVEN+SSM(Spring+SpringMVC+MyBatis)框架
- IntelliJ IDEA搭建Spring环境
- IntelliJ IDEA spring mvc +mybatis 环境搭建服务器(中)
- IntelliJ IDEA spring mvc +mybatis 环境搭建服务器(下)
- Intellij IDEA 搭建Spring MVC & MyBatis开发环境
- 搭建IntelliJ IDEA+maven+jetty+SpringMVC 开发环境(一)
- 搭建IntelliJ IDEA+maven+jetty+SpringMVC 开发环境(一)
- java中ArrayList中为什么elementData使用了transient
- NSString Class Refernce(Objctive-C)
- Android向服务器提交文件
- Yii 配置文件
- spring MVC与ajax通信
- 在IntelliJ IDEA上搭建Spring+SpringMVC+Mybatis+Shiro环境搭建(一)
- 支持向量机(Support Vector Machine)-----SVM之SMO算法(转)
- 微信企业号开发(六)---邀请成员关注
- curry in scala
- xshell中图标按钮含义讲解
- 基于单边jacobi的奇异值分解(SVD)
- POJ-OPENJUDGE-密码翻译
- spark on yarn中yarn-cluster与yarn-client区别
- hdu 1847 Good Luck in CET-4 Everybody! 动态规划/巴什博弈