# shs_simple_framework **Repository Path**: chromeOS/shs-simple-framework ## Basic Information - **Project Name**: shs_simple_framework - **Description**: spring+hibernate+springmvc+shiro 整合 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2016-05-30 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # shs-simple-framework 1. 本项目是采用公司内部底层框架封装,集成了shiro权限控制,整合springmvc+hibernate+spring 暂时支持 mysql数据库,可以扩展. 2.作为内部开源项目展示,目前继承大部分代码逻辑和安全逻辑 **在用solr查询分词高亮的时候用到IK分词器,但是一直报错,错误是 抽象类定义错误, 凭借经验就知道是版本错误的问题,lucene版本为4.10.2 IK版本为 :IK Analyzer 2012FF_hf1.zip 下载地址: https://code.google.com/archive/p/ik-analyzer/downloads** - Sitemesh 介绍和使用 ## 介绍 ### 功能基础编辑 >Sitemesh是由一个基于Web页面布局、装饰及与现存Web应用整合的框架。它能帮助我们在由大量页面工程的项目中创建一致的页面布局和外观,如一致的导航条、一致的banner、一致的版权等。它不仅能处理动态的内容,如JSP、PHP、ASP、CGI等产生的内容,还能处理静态的内容,比如HTML的内容,使得它的内容也符合你的页面结构的要求。甚至它能像include那样将HTML文件作为一个面板的形式嵌入到别的文件中去。所有的这些,都是GOF的Decorator模式的最生动的实现。装饰模式是在不必改变原类文件和使用集成的情况下,动态地扩展一个对象的功能。它能通过创建一个包装对象,也就是装饰来包裹的对象。尽管它是由Java语言来实现的,但是它能与其他Web应用很好的集成。[1] 工作原理编辑 >SiteMesh是基于Servlet的filter的,即过滤流。它是通过截取response,并进行装饰后再交付给客户。 其中涉及到两个名词: 装饰页面(decorator page)和 "被装饰页面(Content page)" , 即 SiteMesh通过对Content Page的装饰,最终得到页面布局和外观一致的页面,并返回给客户 sitemesh运行环境需要:servlet, JDK 。 整合spring maven坐标 ```pom opensymphony sitemesh 2.4.2 ``` 由于采用的是过滤器配置 所有在web.xml中配置 ```xml sitemeshFilter com.opensymphony.sitemesh.webapp.SiteMeshFilter sitemeshFilter /a/* sitemeshFilter /f/* ``` 需要配置文件 在WEB-INF下创建 decorators.xml 内容: ```xml ``` 内容多变 这里用最基本的方式 views/layouts/blank.jsp 内容: ```html <%@ page contentType="text/html;charset=UTF-8"%> <%@ include file="/WEB-INF/views/include/taglib.jsp"%> <%@ taglib prefix="sitemesh" uri="http://www.opensymphony.com/sitemesh/decorator"%> <sitemesh:title /> <%@include file="/WEB-INF/views/include/head_index.jsp"%>
<%@include file="/WEB-INF/views/include/top_blue.jsp"%>
<%@include file="/WEB-INF/views/include/footer_blue.jsp"%>
``` 使用方法 在一个新的jsp中 只要指定 ```html <%@ page contentType="text/html;charset=UTF-8" %> <%@ include file="/WEB-INF/views/include/taglib.jsp" %> Insert title here ``` ....... 1. jquery插件swiper介绍 页面历史 编辑 删除 克隆地址 项目主页:[http://www.swiper.com.cn](http://http://www.swiper.com.cn) 关于Swiper Swiper 是一款免费以及轻量级的移动设备触控滑块的js框架,使用硬件加速过渡(如果该设备支持的话)。主要使用于移动端的网站、移动web apps,native apps和hybrid apps。主要是为IOS而设计的,同时,在Android、WP8系统也有着良好的用户体验,Swiper从3.0开始不再全面支持PC端。因此,如需在PC上兼容更多的浏览器,可以选择Swiper2.x(甚至支持IE7)。 swiper3.x swiper2.x 移动机制 transform transform或left/top 布局方式 flex或一般布局 一般布局 更新 正在更新 停止更新 最新版本2.7.2 兼容性 部分PC端浏览器,主流移动端浏览器 PC端浏览器,IE7+,部分移动端浏览器 API位置 ```html http://www.swiper.com.cn/api/index.html 引入响应的jquery.js和swiper的js,css文件 一个简单的例子 被装饰(目标)页面title
``` 2. shiro配置详解 spring-shiro.xml 中 最重要的是:shiroFilter 配置如下 ```xml ``` 上面的shiroFilterChainDefinitions 定义如下 ```xml /validateCode = anon /static/** = anon /userfiles/** = anon /login = anon /toLogin = anon /logout = logout /** = user ``` 如果实现登陆,查询用户和验证,自定义Realm extends AuthorizingRealm 重写 ```java protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken authcToken) ``` 验证方法和 ```java AuthorizationInfo doGetAuthorizationInfo( PrincipalCollection principals) ``` 授权方法 实现细节如下: ```java protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken authcToken) throws AuthenticationException { // UsernamePasswordToken对象用来存放提交的登录信息 //可进行加密 UsernamePasswordToken token = (UsernamePasswordToken) authcToken; String username = token.getUsername(); String password = new String(token.getPassword()); User user = new User(); user.setName(username); // 查出是否有此用户 user = userService.findByName(token.getUsername()); if (user != null) { if (user.getPsword().equals(password)) { // 若存在,将此用户存放到登录认证info中 return new SimpleAuthenticationInfo(username, password,getName()); } } throw new UnauthenticatedException(); } ``` 3. spring 启动过程 和加载配置文件 介绍 页面历史 编辑 删除 克隆地址 首先配置的是spring核心监听器 web.xml中 ```xml org.springframework.web.context.ContextLoaderListener ``` 启动参数 父类 org.springframework.web.context.ContextLoader 属性需要一个启动参数 contextConfigLocation ```java /** * Name of servlet context parameter (i.e., {@value}) that can specify the * config location for the root context, falling back to the implementation's * default otherwise. * @see org.springframework.web.context.support.XmlWebApplicationContext#DEFAULT_CONFIG_LOCATION */ public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation"; ``` 需要 参数 contextConfigLocation spring加载配置文件 ```java /** * Initialize the root web application context. */ @Override public void contextInitialized(ServletContextEvent event) { initWebApplicationContext(event.getServletContext()); } /** * Initialize Spring's web application context for the given servlet context, * using the application context provided at construction time, or creating a new one * according to the "{@link #CONTEXT_CLASS_PARAM contextClass}" and * "{@link #CONFIG_LOCATION_PARAM contextConfigLocation}" context-params. * @param servletContext current servlet context * @return the new WebApplicationContext * @see #ContextLoader(WebApplicationContext) * @see #CONTEXT_CLASS_PARAM * @see #CONFIG_LOCATION_PARAM */ public WebApplicationContext initWebApplicationContext(ServletContext servletContext) { if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) { throw new IllegalStateException( "Cannot initialize context because there is already a root application context present - " + "check whether you have multiple ContextLoader* definitions in your web.xml!"); } Log logger = LogFactory.getLog(ContextLoader.class); servletContext.log("Initializing Spring root WebApplicationContext"); if (logger.isInfoEnabled()) { logger.info("Root WebApplicationContext: initialization started"); } long startTime = System.currentTimeMillis(); try { // Store context in local instance variable, to guarantee that // it is available on ServletContext shutdown. if (this.context == null) { this.context = createWebApplicationContext(servletContext); } if (this.context instanceof ConfigurableWebApplicationContext) { ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context; if (!cwac.isActive()) { // The context has not yet been refreshed -> provide services such as // setting the parent context, setting the application context id, etc if (cwac.getParent() == null) { // The context instance was injected without an explicit parent -> // determine parent for root web application context, if any. ApplicationContext parent = loadParentContext(servletContext); cwac.setParent(parent); } configureAndRefreshWebApplicationContext(cwac, servletContext); } } servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); ClassLoader ccl = Thread.currentThread().getContextClassLoader(); if (ccl == ContextLoader.class.getClassLoader()) { currentContext = this.context; } else if (ccl != null) { currentContextPerThread.put(ccl, this.context); } if (logger.isDebugEnabled()) { logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]"); } if (logger.isInfoEnabled()) { long elapsedTime = System.currentTimeMillis() - startTime; logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms"); } return this.context; } catch (RuntimeException ex) { logger.error("Context initialization failed", ex); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex); throw ex; } catch (Error err) { logger.error("Context initialization failed", err); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err); throw err; } } protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information String idParam = sc.getInitParameter(CONTEXT_ID_PARAM); if (idParam != null) { wac.setId(idParam); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath())); } } wac.setServletContext(sc); String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM); if (configLocationParam != null) { wac.setConfigLocation(configLocationParam); } // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(sc, null); } customizeContext(sc, wac); wac.refresh(); } ``` 所以我们要在web启动时配置参数 web.xml 通配所有 即:所有启动配置文件都要 spring-xxx.xml样式 ```xml contextConfigLocation classpath*:spring-*.xml ``` 接下来配置spring mvc 核心控制器 org.springframework.web.servlet.DispatcherServlet 这是一个servlet,所以配置很简单 ```xml springMVC org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath*:spring-mvc.xml 1 springMVC / ``` 启动级别设置为1 优先启动并加载 spring-mvc.xml 文件 为了方便编码 我们在设置一个 编码过滤器 统一编码 UTF-8 ```xml encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true encodingFilter /* ``` 我们看spring-mvc.xml配置 注意头文件 ```xml text/plain;charset=UTF-8 ```