`
raymond.chen
  • 浏览: 1417680 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

自定义标签:在JSP页面中动态执行Spring Bean的方法

阅读更多

     使用该自定义标签,可以在JSP页面中动态执行某个Spring Bean对象的一个方法,方法返回的结果存储在ValueStack中。该自定义标签在Spring2、Struts2、Hibernate3环境下测试通过。

 

一、java源代码

    1、ServiceTag源代码

public class ServiceTag extends BaseBodyTagSupport {
	private String beanName;
	private String methodName;
	private String id;
	public List params = new ArrayList();
	
	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}
	
	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	public int doEndTag() throws JspException {		
		Object bean = null;
		Method method = null;
		
		//取得bean对象
		try{
			bean = SpringContextUtil.getBean(beanName);
		}catch(Exception ex){
			throw new JspException("get bean error: " + beanName);
		}
		
		//通过反射取得方法
		if(bean != null){
			try {
				method = bean.getClass().getMethod(methodName, TagUtil.getParameterTypes(params));
			}catch(Exception e) {
				throw new JspException("get method error: " + beanName + "." + methodName);
			}
		}else{
			throw new JspException("ServiceTag Error: bean [" + beanName + "] is null");
		}
		
		//通过反射执行方法,得到结果
		Object result = null;
		if(method != null){
			try {
				result = method.invoke(bean, TagUtil.getParameterValues(params));
			}catch(Exception e){
				throw new JspException("method invoke error");
			}
		}
		
		//将结果存储在ValueStack中
		ValueStack vs = TagUtils.getStack(pageContext);
		vs.getContext().put(id, result);
		
		return EVAL_PAGE;
	}
}

 

    2、ServiceParamTag源代码

public class ServiceParamTag extends BaseBodyTagSupport {
	private String name;
	private Object value;
	private String type;
	
	public void setName(String name) {
		this.name = name;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int doEndTag() throws JspException {
		Tag parent = getParent();
		if(parent instanceof ServiceTag){
			Map p = new HashMap();
			p.put("paramName", name);
			p.put("paramValue", value);
			p.put("paramType", type);
			((ServiceTag)parent).params.add(p);
		}
		
		return EVAL_PAGE;
	}
}

 

    3、公共方法源代码

//参数类型数组
public static Class[] getParameterTypes(List params) throws Exception{
	Class[] c = new Class[params.size()];
	
	for(int i=0;i<params.size();i++){
		Map p = (Map)params.get(i);
		String type = (String)p.get("paramType");
		c[i] = Class.forName(type);
	}
	
	return c;
}

//参数值数组
public static Object[] getParameterValues(List params) throws Exception{
	Object[] o = new Object[params.size()];
	
	for(int i=0;i<params.size();i++){
		Map p = (Map)params.get(i);
		o[i] = p.get("paramValue");
	}
	
	return o;
}

 

二、tld文件源代码

<tag>
	<name>serviceBean</name>
	<tag-class>com.cjm.web.taglib.ServiceTag</tag-class>
	<body-content>JSP</body-content>
	<attribute>
		<name>beanName</name>
		<required>true</required>
		<rtexprvalue>true</rtexprvalue>
	</attribute>
	<attribute>
		<name>methodName</name>
		<required>true</required>
		<rtexprvalue>true</rtexprvalue>
	</attribute>
	<attribute>
		<name>id</name>
		<required>true</required>
		<rtexprvalue>true</rtexprvalue>
	</attribute>
</tag>

<tag>
	<name>serviceParam</name>
	<tag-class>com.cjm.web.taglib.ServiceParamTag</tag-class>
	<body-content>JSP</body-content>
	<attribute>
		<name>name</name>
		<required>true</required>
		<rtexprvalue>true</rtexprvalue>
	</attribute>
	<attribute>
		<name>value</name>
		<required>true</required>
		<rtexprvalue>true</rtexprvalue>
	</attribute>
	<attribute>
		<name>type</name>
		<required>true</required>
		<rtexprvalue>true</rtexprvalue>
	</attribute>
</tag>

 

三、范例

    1、java源代码

public class RoleService extends BaseService {
	private RoleDao roleDao;
	
	public RoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public Role getRole(String roleId){
		return (Role)get(Role.class, roleId);
	}
}

    2、JSP页面源代码

<cjm:serviceBean beanName="roleService" methodName="getRole" id="result">
	<cjm:serviceParam name="roleId" value="ADMIN" type="java.lang.String"/>
</cjm:serviceBean>

<s:property value="#result.roleName"/>

 

2
1
分享到:
评论
2 楼 itc10 2015-10-21  
//取得bean对象 
24.        try{ 
25.            bean = SpringContextUtil.getBean(beanName); 
26.        }catch(Exception ex){ 
27.            throw new JspException("get bean error: " + beanName); 
28.        } 


SpringContextUtil  ????,再加载applicationContext.xml一次??这在web项目会不会造成两个spring容器?,我也是醉了。
1 楼 dayang2001911 2008-10-07  
你也可以用ajax方式来实现吧,或者用dwr框架,直接就和spring集成的,可以调用bean的方法的啊,当然你这么做也应该可以了。

相关推荐

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Spring in Action(第二版 中文高清版).part2

    16.4.3 在JSF页面中使用Spring Bean 16.4.4 在JSF中暴露应用程序环境 16.5 Spring中带有DWR的支持Ajax的应用程序 16.5.1 直接Web远程控制 16.5.2 访问Spring管理的Bean DWR 16.6 小结 附录A 装配Spring A.1 ...

    Spring in Action(第二版 中文高清版).part1

    16.4.3 在JSF页面中使用Spring Bean 16.4.4 在JSF中暴露应用程序环境 16.5 Spring中带有DWR的支持Ajax的应用程序 16.5.1 直接Web远程控制 16.5.2 访问Spring管理的Bean DWR 16.6 小结 附录A 装配Spring A.1 ...

    Spring 2.0 开发参考手册

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Spring in Action(第2版)中文版

    16.4.3在jsf页面中使用springbean 16.4.4在jsf中暴露应用程序环境 16.5spring中带有dwr的支持ajax的应用程序 16.5.1直接web远程控制 16.5.2访问spring管理的beandwr 16.6小结 附录a装配spring a.1下载spring ...

    spring chm文档

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Spring.3.x企业应用开发实战(完整版).part2

    Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。  Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    ssh(structs,spring,hibernate)框架中的上传下载

     文件数据存储在Blob类型的FILE_CONTENT表字段上,在Spring中采用OracleLobHandler来处理Lob字段(包括Clob和Blob),由于在程序中不需要引用到oracle数据驱动程序的具体类且屏蔽了不同数据库处理Lob字段方法上的...

    Spring3.x企业应用开发实战(完整版) part1

    Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。  Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...

    spring security 参考手册中文版

    12.2.3使用RequestPostProcessor在Spring MVC测试中以用户身份运行 106 作为用户在Spring MVC测试中使用注释运行 108 12.2.4测试HTTP基本认证 109 12.3 SecurityMockMvcRequestBuilders 109 12.3.1测试基于表单的...

    从零开始学Spring Boot

    1.20 Spring Boot普通类调用bean 1.21 使用模板(thymeleaf-freemarker) 1.22 Spring Boot 添加JSP支持 1.23 Spring Boot Servlet 1.24 Spring Boot过滤器、监听器 1.25 Spring Boot 拦截器HandlerInterceptor 1.26...

    完整版Java web开发教程PPT课件 Java开发进阶教程 第10章 过滤器、监听器、自定义标签(共19页).pptx

    完整版Java web开发教程PPT课件 Java开发进阶教程 第10章 过滤器、监听器、自定义标签(共19页).pptx 完整版Java web开发教程PPT课件 Java开发进阶教程 第11章 AJAX实现(共11页).pptx 完整版Java web开发教程PPT...

    java面试题

    jsp中动态include和静态include的区别? 答:动态include用jsp:include实现,适用于动态页面,可以携带参数 静态include用include伪码实现,适用于静态页面 math.round(11.5)等于多少?math.round(-11.5)等于多少? ...

    Spring面试题

    在 XML 文件中定义的 Bean 是被消极加载的,这意味在需要 bean 之前,bean 本身不会被初始化。要从 BeanFactory 检索 bean,只需调用 getBean() 方法,传入将要检索的 bean 的名称即可,如清单 2 所示。 清单 2. ...

    基于Spring MVC的web框架 1.1.11

    工具类数据校验 jsp自定义标签 Spring自定义注解 默认requestMapping 1.1.2 代码生成器 1.1.3 首页修改 dateformat.js 时间参数转换 SpringMVC配置文件集中 快递参数接口 1.1.4 des加解密字符串和文件 1.1.5 redis...

    在action以外的地方获取dao

    这种方法通常在写组件时用,比如写个自定义标签,正常的流程,最后都走action,尽量保证每个请求都给一个.do,而不是直接去一个JSP,这样既保证了MVC模式,又防止用户直接记下JSP地址在没有后台准备情况下访问。

    低清版 大型门户网站是这样炼成的.pdf

    2.5.1 在action中实现手动校验 90 2.5.2 调用校验框架进行自动校验 91 2.5.3 自定义国际化struts 2校验错误消息 92 2.5.4 struts 2的自带校验器 92 2.6 struts 2的拦截器 94 2.6.1 struts 2内建拦截器介绍 95 ...

    Spring Security-3.0.1中文官方文档(翻译版)

    20. JSP 标签库 20.1. 声明Taglib 20.2. authorize 标签 20.3. authentication 标签 20.4. accesscontrollist 标签 21. Java 认证和授权服务(JAAS)供应器 21.1. 概述 21.2. 配置 21.2.1. JAAS ...

Global site tag (gtag.js) - Google Analytics