1. 程式人生 > >Spring MVC整合velocity擴充套件

Spring MVC整合velocity擴充套件

1、擴充套件velocity的檢視
[code="java"]package org.christ.matrix.template.velocity;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.Scope;
import org.apache.velocity.tools.Toolbox;
import org.apache.velocity.tools.ToolboxFactory;
import org.apache.velocity.tools.config.XmlFactoryConfiguration;
import org.apache.velocity.tools.view.ViewToolContext;
import org.christ.matrix.template.TemplateVariable;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.support.ServletContextResource;
import org.springframework.web.servlet.view.velocity.VelocityToolboxView;

/**
* 擴充套件velocity的檢視解析器。
*
* 自動匹配到Layout並擴充套件相關模板工具的Velocity檢視。
*
* @author 劉飛 E-mail:
[email protected]

* @version 1.0
* @since 2013-7-1 上午11:06:59
*/
public class VelocityTemplateLayoutView extends VelocityToolboxView implements DefaultVelocityConfigurer {

/** 模板檔案的根路徑目錄 */
protected String templates = null;
/** screen模板解析的檢視目錄 */
protected String screen = null;
/** layout模板解析的檢視目錄 */
protected String layout = null;

/** velocity模板預設載入的layout模板 */
protected String defaultLayoutTemplate = null;
/** screen模板key */
protected String screenTemplateKey = null;

/** 本次請求對應的檢視名稱 */
protected String viewName = null;
/** 模板對應的副檔名稱 */
protected String suffix = null;

protected List toolboxs = new ArrayList();

@Override
protected void doRender(Context context, HttpServletResponse response) throws Exception {
Template screenTemplate = getTemplate();
// 同名的layout
String layoutTemplateURL = SYSTEM_SEPARATOR + templates + SYSTEM_SEPARATOR + layout + SYSTEM_SEPARATOR
+ viewName + suffix;
Template layoutTemplate = safeLoadLayoutTemplate(layoutTemplateURL);
if (layoutTemplate == null) {// 預設的layout
layoutTemplateURL = SYSTEM_SEPARATOR + templates + SYSTEM_SEPARATOR + layout + SYSTEM_SEPARATOR
+ defaultLayoutTemplate;
layoutTemplate = safeLoadLayoutTemplate(layoutTemplateURL);
}
if (layoutTemplate == null) {// 沒有找到layout就只解析screen
mergeTemplate(screenTemplate, context, response);
} else {
context.put(screenTemplateKey, templateRender(context, screenTemplate));
mergeTemplate(layoutTemplate, context, response);
}
}

@Override
protected Context createVelocityContext(Map model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
ViewToolContext context = new ViewToolContext(getVelocityEngine(), request, response, getServletContext());
context.putAll(model);

if (!CollectionUtils.isEmpty(toolboxs)) {// 注入模板工具
for (Toolbox toolbox : toolboxs) {
context.addToolbox(toolbox);
}
}

Map templateVariables = getApplicationContext().getBeansWithAnnotation(TemplateVariable.class);

if (templateVariables != null && templateVariables.size() > 0) {
for (Map.Entry e : templateVariables.entrySet()) {// 注入自定義工具
context.put(e.getKey(), e.getValue());
}
}

Map controls = getApplicationContext().getBeansOfType(
VelocityTemplateController.class, true, false);
if (controls != null && controls.size() > 0) {// 注入control
for (Map.Entry e : controls.entrySet()) {
String name = e.getKey();
VelocityTemplateController controller = e.getValue();
controller.setContext(context);
controller.setVelocityEngine(getVelocityEngine());
context.put(name, controller);
}
}

return context;
}

/**
* 初始化工作。
*
* @throws Exception
*/
protected void init() throws Exception {
if (StringUtils.isBlank(templates)) {
templates = DEFAULT_TEMPLATES;
}
if (StringUtils.isBlank(screen)) {
screen = DEFAULT_SCREEN;
}
if (StringUtils.isBlank(layout)) {
layout = DEFAULT_LAYOUT;
}

if (StringUtils.isBlank(defaultLayoutTemplate)) {
defaultLayoutTemplate = DEFAULT_LAYOUT_TEMPLATE;
}
if (StringUtils.isBlank(suffix)) {
suffix = DEFAULT_SUFFIX;
}
if (StringUtils.isBlank(screenTemplateKey)) {
screenTemplateKey = DEFAULT_SCREEN_TEMPLATE_KEY;
}

if (StringUtils.isNotBlank(getToolboxConfigLocation())) {
// 擴充套件使用velocity tools 2.0
try {
XmlFactoryConfiguration configuration = new XmlFactoryConfiguration();
ServletContextResource resource = new ServletContextResource(getServletContext(),
getToolboxConfigLocation());
configuration.read(resource.getURL());
ToolboxFactory factory = configuration.createFactory();
if (factory.hasTools(Scope.APPLICATION)) {
Toolbox applicationTool = factory.createToolbox(Scope.APPLICATION);
if (applicationTool != null) {
toolboxs.add(applicationTool);
}
}
if (factory.hasTools(Scope.REQUEST)) {
Toolbox requestTool = factory.createToolbox(Scope.REQUEST);
if (requestTool != null) {
toolboxs.add(requestTool);
}
}
if (factory.hasTools(Scope.SESSION)) {
Toolbox sessionTool = factory.createToolbox(Scope.SESSION);
if (sessionTool != null) {
toolboxs.add(sessionTool);
}
}
} catch (Exception e) {
logger.error(String.format("init toolbox [%s] error.", getToolboxConfigLocation()), e);
}
}
}

/**
* 安全的校驗並獲取layout模板。
*
* @param layoutURL
* @return
*/
protected Template safeLoadLayoutTemplate(String layoutURL) {
try {
return getTemplate(layoutURL);
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("Loading Layout Template: " + layoutURL, e);
}
}
return null;
}

@Override
public final void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();
try {
init();
} catch (Exception e) {
logger.error("VelocityTemplateLayoutView#init error.", e);
}
}

@Override
public boolean checkResource(Locale locale) throws Exception {

if (StringUtils.isBlank(templates)) {
if (logger.isErrorEnabled()) {
logger.error("Property 'templates' is required.");
}
throw new IllegalArgumentException("Property 'templates' is required.");
}
if (StringUtils.isBlank(screen)) {
if (logger.isErrorEnabled()) {
logger.error("Property 'screen' is required.");
}
throw new IllegalArgumentException("screen 'screen' is required.");
}
if (StringUtils.isBlank(layout)) {
if (logger.isErrorEnabled()) {
logger.error("Property 'layout' is required.");
}
throw new IllegalArgumentException("screen 'layout' is required.");
}

if (StringUtils.isBlank(defaultLayoutTemplate)) {
if (logger.isErrorEnabled()) {
logger.error("Property 'defaultLayoutTemplate' is required.");
}
throw new IllegalArgumentException("screen 'defaultLayoutTemplate' is required.");
}
if (StringUtils.isBlank(suffix)) {
if (logger.isErrorEnabled()) {
logger.error("Property 'suffix' is required.");
}
throw new IllegalArgumentException("screen 'suffix' is required.");
}
if (StringUtils.isBlank(screenTemplateKey)) {
if (logger.isErrorEnabled()) {
logger.error("Property 'screenTemplateKey' is required.");
}
throw new IllegalArgumentException("screen 'screenTemplateKey' is required.");
}

if (StringUtils.isBlank(viewName)) {
if (logger.isErrorEnabled()) {
logger.error("Property 'viewName' is required.");
}
throw new IllegalArgumentException("screen 'viewName' is required.");
}

return super.checkResource(locale);
}

/**
* 渲染一個模板。
*
* @param context
* @param template
* @return
*/
protected String templateRender(Context context, Template template) {
if (template == null || context == null) {
return null;
}
try {
StringWriter sw = new StringWriter();
template.merge(context, sw);
return sw.toString();
} catch (Exception e) {
logger.error(String.format("Template[%s] Render Error.", template.getName()), e);
}
return null;
}

/**
* @return the templates
*/
public String getTemplates() {
return templates;
}

/**
* @param templates
* the templates to set
*/
public void setTemplates(String templates) {
this.templates = templates;
}

/**
* @return the screen
*/
public String getScreen() {
return screen;
}

/**
* @param screen
* the screen to set
*/
public void setScreen(String screen) {
this.screen = screen;
}

/**
* @return the layout
*/
public String getLayout() {
return layout;
}

/**
* @param layout
* the layout to set
*/
public void setLayout(String layout) {
this.layout = layout;
}

/**
* @return the defaultLayoutTemplate
*/
public String getDefaultLayoutTemplate() {
return defaultLayoutTemplate;
}

/**
* @param defaultLayoutTemplate
* the defaultLayoutTemplate to set
*/
public void setDefaultLayoutTemplate(String defaultLayoutTemplate) {
this.defaultLayoutTemplate = defaultLayoutTemplate;
}

/**
* @return the viewName
*/
public String getViewName() {
return viewName;
}

/**
* @param viewName
* the viewName to set
*/
public void setViewName(String viewName) {
this.viewName = viewName;
}

/**
* @return the suffix
*/
public String getSuffix() {
return suffix;
}

/**
* @param suffix
* the suffix to set
*/
public void setSuffix(String suffix) {
this.suffix = suffix;
}

/**
* @return the screenTemplateKey
*/
public String getScreenTemplateKey() {
return screenTemplateKey;
}

/**
* @param screenTemplateKey
* the screenTemplateKey to set
*/
public void setScreenTemplateKey(String screenTemplateKey) {
this.screenTemplateKey = screenTemplateKey;
}
}
[/code]


2、擴充套件velocity模板解析器來完成上述的規約配置。
[code="java"]/**
* E-Mail :
[email protected]
, [email protected], [email protected]
* QQ : 970275153
*/
package org.christ.matrix.template.velocity;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.christ.matrix.template.TemplateViewResolver;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.io.ClassRelativeResourceLoader;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.context.support.ServletContextResourceLoader;
import org.springframework.web.servlet.view.AbstractUrlBasedView;
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;

/**
* 模板檢視佈局規則:
*
* 指定模板檔案的根路徑目錄,在該目錄的子目錄下指定一個目錄作為screen模板解析的檢視目錄
*
* 在該目錄的子目錄下指定一個目錄作為layout模板解析的檢視目錄
*
* 其餘的子目錄下的模板直接用其目錄名稱作為control物件直接引用即可。
*
* @author 劉飛 E-mail:
[email protected]

* @version 1.0
* @since 2013-6-30 下午01:36:58
*/
public class VelocityTemplateLayoutViewResolver extends VelocityViewResolver implements TemplateViewResolver,
DefaultVelocityConfigurer {
/** 載入control模板檔案的編碼 */
protected String encoding = null;
/** 模板檔案的根路徑目錄 */
protected String templates = null;
/** screen模板解析的檢視目錄 */
protected String screen = null;
/** layout模板解析的檢視目錄 */
protected String layout = null;

/** velocity模板預設載入的layout模板 */
protected String defaultLayoutTemplate = null;
/** screen模板key */
protected String screenTemplateKey = null;

/** 資源載入器。 */
protected ResourceLoader resourceLoader = new DefaultResourceLoader();
/** {@link #templates} 下的control目錄 */
protected final List controls = new ArrayList();

/** velocity模板contentType */
protected String contentType = null;
/** 約定:模板快取上限,為-1的時候表示關閉,不配置的話預設是2048 */
protected Integer cacheLimit = null;

@Override
protected Class> requiredViewClass() {
return VelocityTemplateLayoutView.class;
}

@Override
protected AbstractUrlBasedView buildView(String viewName) throws Exception {
VelocityTemplateLayoutView view = VelocityTemplateLayoutView.class.cast(super.buildView(viewName));
view.setCacheTemplate(true);
view.setContentType(contentType);
view.setTemplates(templates);
view.setScreen(screen);
view.setLayout(layout);
view.setEncoding(encoding);
view.setViewName(viewName);
view.setSuffix(getSuffix());
view.setScreenTemplateKey(screenTemplateKey);
view.setDefaultLayoutTemplate(defaultLayoutTemplate);
return view;
}

protected void controlBeanRegistry(BeanDefinitionRegistry registry) throws Exception {
if (controls == null || controls.isEmpty()) {
logger.debug("沒有control需要配置.");
return;
}
for (File controlFile : controls) {
String absolutePath = controlFile.getAbsolutePath();
String name = controlFile.getName();
try {
RootBeanDefinition controlBean = getDefaultRootBeanDefinition(VelocityTemplateController.class,
"模板引擎擴充套件的control:" + name);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("encoding", encoding);
/** 將control的絕對路徑賦予 */
propertyValues.addPropertyValue("control", SYSTEM_SEPARATOR + templates + SYSTEM_SEPARATOR + name
+ SYSTEM_SEPARATOR);
controlBean.setPropertyValues(propertyValues);
registry.registerBeanDefinition(name, controlBean);
logger.error(String.format("Registry control Named[%s] for template directory[%s] success.", name,
absolutePath));
} catch (Exception e) {
logger.error(String.format("Registry control Named[%s] for template directory[%s] error.", name,
absolutePath));
}
}
}

@Override
public void afterPropertiesSet() throws Exception {
if (StringUtils.isBlank(templates)) {
templates = DEFAULT_TEMPLATES;
}
if (StringUtils.isBlank(screen)) {
screen = DEFAULT_SCREEN;
}
if (StringUtils.isBlank(layout)) {
layout = DEFAULT_LAYOUT;
}
if (StringUtils.isBlank(encoding)) {
encoding = DEFAULT_ENCODING;
}

if (StringUtils.isBlank(defaultLayoutTemplate)) {
defaultLayoutTemplate = DEFAULT_LAYOUT_TEMPLATE;
}
if (StringUtils.isBlank(screenTemplateKey)) {
screenTemplateKey = DEFAULT_SCREEN_TEMPLATE_KEY;
}

if (StringUtils.isEmpty(contentType)) {
contentType = DEFAULT_CONTENT_TYPE;
}

if (StringUtils.isBlank(getSuffix())) {//預設配置
setSuffix(DEFAULT_SUFFIX);
}

setPrefix(SYSTEM_SEPARATOR + templates + SYSTEM_SEPARATOR + screen + SYSTEM_SEPARATOR);// 設定prefix

if (cacheLimit == null) {
cacheLimit = CACHE_LIMIT;
}
if(cacheLimit != null && cacheLimit > 0) {
setCacheLimit(cacheLimit);
setCache(true);
} else {
setCache(false);
}

try {
/** 模板根路徑 */
File templates = getTemplatesSource(SYSTEM_SEPARATOR + this.templates + SYSTEM_SEPARATOR);
if (templates != null && templates.isDirectory()) {
/** 過濾根路徑下的control目錄 */
File[] controlTemplates = templates.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
if (StringUtils.equalsIgnoreCase(name, screen) || StringUtils.equalsIgnoreCase(name, layout)) {
return false;
}
return true;
}
});
if (ArrayUtils.isNotEmpty(controlTemplates)) {
controls.addAll(Arrays.asList(controlTemplates));
}
}
} catch (Exception e) {
logger.error("載入control異常", e);
}
}

/**
* 獲取模板根路徑檔案。
*
* @param templates
* @return
*/
protected File getTemplatesSource(String templatesPath) {
File templates = null;
try {
templates = resourceLoader.getResource(templatesPath).getFile();
if(templates != null) {
return templates;
}
} catch (Exception e) {
logger.warn(String.format("[%s] trying loading resource [%s] failure.", resourceLoader.getClass().getSimpleName(), templatesPath), e);
}
try {
templates = new ServletContextResourceLoader(getServletContext()).getResource(templatesPath).getFile();
if(templates != null) {
return templates;
}
} catch (Exception e) {
logger.error(String.format("[%s] trying loading resource [%s] failure.", ServletContextResourceLoader.class.getSimpleName(), templatesPath), e);
}
try {
templates = new ClassRelativeResourceLoader(getClass()).getResource(templatesPath).getFile();
if(templates != null) {
return templates;
}
} catch (Exception e) {
logger.error(String.format("[%s] trying loading resource [%s] failure.", ClassRelativeResourceLoader.class.getSimpleName(), templatesPath), e);
}

logger.error(String.format("trying loading resource [%s] failure.", templatesPath));
return null;
}

/**
* 在這裡將注入和各個目錄對應的control
*/
@Override
public final void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
try {
if (beanFactory instanceof BeanDefinitionRegistry) {
controlBeanRegistry(BeanDefinitionRegistry.class.cast(beanFactory));
} else {
logger.warn("ConfigurableListableBeanFactory can't be cast to BeanDefinitionRegistry.");
}
postProcessBeanFactory0(beanFactory);
} catch (Exception e) {
logger.error("注入和各個目錄對應的control異常", e);
}
}

/**
* 基礎的RootBeanDefinition定義。
*
* @param clazz
* @param description
* @return
*/
protected RootBeanDefinition getDefaultRootBeanDefinition(Class> clazz, String description) {
RootBeanDefinition beanDefinition = new RootBeanDefinition(clazz);
beanDefinition.setAbstract(false);
beanDefinition.setAutowireCandidate(true);
beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
beanDefinition.setDependencyCheck(AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);
beanDefinition.setDescription(description);
beanDefinition.setLazyInit(false);
beanDefinition.setScope(AbstractBeanDefinition.SCOPE_SINGLETON);
return beanDefinition;
}

protected void postProcessBeanFactory0(ConfigurableListableBeanFactory beanFactory) throws Exception {
}

@Override
public void setTemplates(String templates) {
this.templates = templates;

}

@Override
public void setScreen(String screen) {
this.screen = screen;
}

@Override
public void setLayout(String layout) {
this.layout = layout;

}

/**
* @param resourceLoader
* the resourceLoader to set
*/
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}

/**
* @param encoding
* the encoding to set
*/
public void setEncoding(String encoding) {
this.encoding = encoding;
}

/**
* @return the defaultLayoutTemplate
*/
public String getDefaultLayoutTemplate() {
return defaultLayoutTemplate;
}

/**
* @param defaultLayoutTemplate
* the defaultLayoutTemplate to set
*/
public void setDefaultLayoutTemplate(String defaultLayoutTemplate) {
this.defaultLayoutTemplate = defaultLayoutTemplate;
}

/**
* @return the screenTemplateKey
*/
public String getScreenTemplateKey() {
return screenTemplateKey;
}

/**
* @param screenTemplateKey
* the screenTemplateKey to set
*/
public void setScreenTemplateKey(String screenTemplateKey) {
this.screenTemplateKey = screenTemplateKey;
}

/**
* @return the encoding
*/
public String getEncoding() {
return encoding;
}

/**
* @return the templates
*/
public String getTemplates() {
return templates;
}

/**
* @return the screen
*/
public String getScreen() {
return screen;
}

/**
* @return the layout
*/
public String getLayout() {
return layout;
}

/**
* @return the resourceLoader
*/
public ResourceLoader getResourceLoader() {
return resourceLoader;
}
}
[/code]

重新定義的解析器介面
[code="java"]/**
* E-Mail : [email protected], [email protected], [email protected]
* QQ : 970275153
*/
package org.christ.matrix.template;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;

/**
* 模板檢視佈局規則:
*
* 指定模板檔案的根路徑目錄,在該目錄的子目錄下指定一個目錄作為screen模板解析的檢視目錄
*
* 在該目錄的子目錄下指定一個目錄作為layout模板解析的檢視目錄
*
* 其餘的子目錄下的模板直接用其目錄名稱作為control物件直接引用即可。
*
* @author 劉飛 E-mail:[email protected]
* @version 1.0
* @since 2013-6-30 上午11:58:41
*/
public interface TemplateViewResolver extends BeanFactoryPostProcessor, InitializingBean {

/**
* 指定模板檔案的根路徑目錄.
*
* @param templates
*/
void setTemplates(String templates);

/**
* 指定{@link #setTemplate(String)}一個目錄作為screen模板解析的檢視目錄.
*
* @param screen
*/
void setScreen(String screen);

/**
* 指定{@link #setTemplate(String)}一個目錄作為layout模板解析的檢視目錄
*
* @param layout
*/
void setLayout(String layout);
}
[/code]

相關推薦

Spring MVC整合velocity擴充套件

1、擴充套件velocity的檢視[code="java"]package org.christ.matrix.template.velocity;import java.io.StringWriter;import java.util.ArrayList;import ja

Spring MVC整合Velocity

java程序 界面設計 配置文件 web開發 engine Velocity模板(VM)語言介紹Velocity是一個基於java的模板引擎(template engine)。它允許任何人僅僅簡單的使用模板語言(template language)來引用由java代碼定義的對象。當Velo

Spring MVC整合Velocity詳解

springmvcvelocity一、Velocity簡介Velocity是一個基於java的模板引擎(template engine)。它允許任何人僅僅簡單的使用模板語言(template language)來引用由java代碼定義的對象。當Velocity應用於web開發時,界面設計人員可以和java程序

2、Spring MVC整合Velocity

Spring MVC整合Velocity,就要引入相關的包,需要匯入的包如下圖 然後,就是進行很簡單的配置了,那在這裡,就修改下springMvc-servlet.xml內容,內容修改如下 <?xml version="1.0" encoding="UTF-8"

Spring MVC 整合Velocity及用法說明

Velocity是一個基於java的模板引擎(template engine),它允許任何人僅僅簡單的使用模板語言(template language)來引用由java程式碼定義的物件。 配置: 1.在pom.xml增加依賴的velocity包 <dependenc

Spring MVC 整合 Velocity

當spring mvc 需要整合Velocity 時:application.xml中配置如下: <!-- 模板資訊設定 --> <bean id="velocityConfigurer" class="org.springfram

spring mvc整合velocity原始碼分析

一、首先複習下velocity模板的使用 package com.springmvc.controller; import java.io.BufferedWriter; import java.io.OutputStreamWriter; import java.ut

Spring MVC整合fastjson、EasyUI亂碼問題

pri har value object 解決 pub return urn ast 一、框架版本 Spring MVC:spring-webmvc-4.0.0.RELEASE fastjson:fastjson-1.2.45 EasyUI:1.5 二、亂碼現象

spring mvc整合Quartz框架配置多定時任務

一、增加所依賴的JAR包     1、增加Spring的Maven依賴 <!--Quartz的Maven依賴 --> <dependency> <groupId>org.quartz-scheduler</group

Spring mvc整合freemarker詳解

1.什麼是FreeMarker FreeMarker是一個模板引擎,一個基於模板生成文字輸出的通用工具,使用純Java編寫 FreeMarker被設計用來生成HTML Web頁面,特別是基於MVC模式的應用程式 雖然FreeMarker具有一些程式設計的能力,但

Spring MVC 整合 Logback

引用slf4j和logback的依賴,尤其是logback-ext-spring,是Logback對於Spring的擴充套件: <!-- Logging begin --> <dependency> <groupId>org.

springspring MVC整合

spring和springMVC之間的整合,springMVC中的jar包包含spring中的jar包,所以無需再另外匯入jar包,只需匯入springMVC的jar包即可。 如圖一所示: 這個時候,再新建兩個原始檔夾,一個為config專門放配置檔案,另外一個為test

Spring MVC學習總結(13)——Spring MVC整合Swagger時文件無法排序問題

新增排序屬性: window.swaggerUi = new SwaggerUi({      ...      apisSorter: "alpha", // can also be a funct

mybatis註解實現 spring mvc + mybatis+velocity 框架 (附完整專案程式碼)

最近學習了一下mybatis結合網上的一些列子搭建的一個網站的框架,前端顯示用的velocity引擎,資料訪問使用mybatis+oracle,檢視訪問控制spring mvc,框架主要包括一些基本的增刪改操作以及攔截器,廢話不多說了直接開始吧!! 程式碼下載

Spring MVC配置Velocity

Velocity是一種Java模板引擎。 和JSP,Freemarker差不多,都是用來展示網頁內容的。 和JSP不同的是velocity只能顯示Action中的資料,不能處理資料。不能寫java程式碼,但是可以使用Velocity標記。 Velocity的頁面(模版)可是

dubbo與Spring MVC整合

注意事項: 1、注意dubbo與自有maven工程的jar依賴(Spring相關及servlet-api) 2、dubbo版本2.8.4 參考資料: 1、https://github.com/dangdangdotcom/dubbox 2、Dubbo與Zookeeper

Spring mvc整合Mybatis,選擇性儲存物件欄位資料

前言 我們平時使用mybatis儲存物件資料時,經常可能只是修改其中某一倆個欄位的值,這個時候,我們為了減少資料庫更新帶來的效能、行鎖等不必要的消耗,可能會重新寫一個介面,只負責修改需要修改的值。 但是,隨著業務系統的變更,業務欄位的增加,越來越多的欄位需要

spring mvc 整合 jcaptcha 生成圖形驗證碼

jcaptcha是一個開源的用來生成驗證碼的Java開源元件 maven依賴 <dependency> <groupId>com.octo.captcha

Spring MVC整合beetl之後新增國際化

實現beetl的function,然後使用spring配置注入該bean即可 public class I18nUtil implements Function{ @Override public Object call(Object[] o

spring mvc 整合shiro 做許可權的簡單使用

1.概述 現在的專案使用的許可權控制系統是spring security 3.因為專案的框架使用spring,就順便使用了。最近研究了一下spring side4,推薦使用shiro。照著示例做了一遍。在原有的spring web工程中。步驟如下。 2.引進包,maven