<% String name="diego"; request.setAttribute("name",name); %> <c:out value="${name}"/> ...... |
package diegoyun; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; import org.apache.commons.beanutils.PropertyUtils; import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager; public class SetVarTag extends TagSupport { private Object value = null; private String property = null; private String var = null; public void setVar(String var) { this.var = var; } public void setProperty(String property) { this.property = property; } public void setValue(Object value)throws JspException{ this.value = ExpressionEvaluatorManager.evaluate( "value", value.toString(), Object.class, this, pageContext); } public int doEndTag() throws JspException{ Object propertyValue = null; try{ propertyValue = PropertyUtils.getProperty(value, property); } catch (Exception e) { throw new JspException(e); } pageContext.setAttribute(var,propertyValue); return EVAL_PAGE; } } |
<!--SetVarTag--> <tag> <name>set</name> <tag-class>diegoyun.SetVarTag</tag-class> <body-content>empty</body-content> <attribute> <name>value</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>property</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>var</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> |
<%@ page language="java" %> <%@ page import="diegoyun.vo.*"%> <%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%> <%@ taglib uri="/WEB-INF/tlds/c.tld" prefix="c"%> <html> <body bgcolor="#FFFFFF"> <% Man man = new Man(); man.setName("diego"); request.setAttribute("man",man); %> Get value from request and set it@#s property value into request:<br> <diego:set value="${man}" property="name" var="myname"/> now use OutTag of jstl taglib to get the name:<br> value is : <c:out value="${myname}" /> </body> </html> |
Get value from request and set it@#s property value into request: now use OutTag of jstl taglib to get the name: value is : diego |