自定义标签:在JSP页面中动态执行Spring Bean的方法
来源:互联网 发布:网络运维投标书 编辑:程序博客网 时间:2024/05/01 08:57
自定义标签:在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;}
<pre>
* 一定要在spring.xml中加上:
* <bean id="SpringContextUtil " class="com.hmw.spring.SpringContextUtil" singleton="true" />
* 当对SpringContextUtil 实例时就自动设置applicationContext,以便后来可直接用applicationContext
* </pre>
- package org.company.xxx;
-
- import org.springframework.beans.BeansException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- /**
- *
- * 获取spring容器,以访问容器中定义的其他bean
- * @author lyltiger
- * @since MOSTsView 3.0 2009-11-16
- */
- public class SpringContextUtil implements ApplicationContextAware {
-
- // Spring应用上下文环境
- private static ApplicationContext applicationContext;
-
- /**
- * 实现ApplicationContextAware接口的回调方法,设置上下文环境
- *
- * @param applicationContext
- */
- public void setApplicationContext(ApplicationContext applicationContext) {
- SpringContextUtil.applicationContext = applicationContext;
- }
-
- /**
- * @return ApplicationContext
- */
- public static ApplicationContext getApplicationContext() {
- return applicationContext;
- }
-
- /**
- * 获取对象
- * 这里重写了bean方法,起主要作用
- * @param name
- * @return Object 一个以所给名字注册的bean的实例
- * @throws BeansException
- */
- public static Object getBean(String name) throws BeansException {
- return applicationContext.getBean(name);
- }
-
- }
二、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"/>
自定义标签:在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;}
<pre>
* 一定要在spring.xml中加上:
* <bean id="SpringContextUtil " class="com.hmw.spring.SpringContextUtil" singleton="true" />
* 当对SpringContextUtil 实例时就自动设置applicationContext,以便后来可直接用applicationContext
* </pre>
- package org.company.xxx;
- import org.springframework.beans.BeansException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- /**
- *
- * 获取spring容器,以访问容器中定义的其他bean
- * @author lyltiger
- * @since MOSTsView 3.0 2009-11-16
- */
- public class SpringContextUtil implements ApplicationContextAware {
- // Spring应用上下文环境
- private static ApplicationContext applicationContext;
- /**
- * 实现ApplicationContextAware接口的回调方法,设置上下文环境
- *
- * @param applicationContext
- */
- public void setApplicationContext(ApplicationContext applicationContext) {
- SpringContextUtil.applicationContext = applicationContext;
- }
- /**
- * @return ApplicationContext
- */
- public static ApplicationContext getApplicationContext() {
- return applicationContext;
- }
- /**
- * 获取对象
- * 这里重写了bean方法,起主要作用
- * @param name
- * @return Object 一个以所给名字注册的bean的实例
- * @throws BeansException
- */
- public static Object getBean(String name) throws BeansException {
- return applicationContext.getBean(name);
- }
- }
二、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"/>
- 自定义标签:在JSP页面中动态执行Spring Bean的方法
- 在JSP页面中调用Spring容器注入的Bean的2种方法
- 在自定义标签子类获取Spring中注册的bean
- 在jsp页面中获取Spring管理的Bean
- 在JSP页面中自定义标签
- jsp中自定义标签的执行过程
- 在struts 自定义标签中注入spring bean
- 如何在JSP页面中调用Spring容器注入的Bean?
- 如何在JSP页面中调用Spring容器注入的Bean?
- JSP页面怎样调用Spring配置文件中定义的Bean
- JSP页面中调用Spring容器注入的Bean
- 自定义标签中获取Spring Bean
- 在自定义Listener中使用Spring容器管理的bean
- JSP之自定义标签的执行过程
- Spring中初始化bean和销毁bean的时候执行某个方法的详解
- Spring中初始化bean和销毁bean的时候执行某个方法的详解
- Spring中初始化bean和销毁bean的时候执行某个方法的详解
- Spring中初始化bean和销毁bean的时候执行某个方法的详解
- HTML标签来格式化文本
- 修改oracle 150 的最大连接数
- ORACLE 最大连接数的问题
- ORACLE查看并修改最大连接数
- OracleOraHome90ManagementServer启动失败
- 自定义标签:在JSP页面中动态执行Spring Bean的方法
- 由Servlet到Struts(一)——之Servlet
- Oracle 11.2.0.2 exp导出错误处理一则
- 打开弹出窗口
- 也谈lwip的回调
- 详解笔记本屏幕
- javascript调用js文件
- 关于ARM 汇编 指令 操作范围的一点理解
- 量变确实可以引起质变