You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mark Lowe <ma...@talk21.com> on 2004/03/02 10:02:20 UTC

JSP context in an action

Does anyone know how to retrieve the servlet context of a given jsp and 
place an object in that context (say a map) and then request the page ?

I've been putting together a email templating system using velocity, 
but logic tells me i can do this using the servlet and struts api's 
without using velocity. Velocity wont let me retrieve the *.vm file 
without extending velocity servlet which i dont see as nessesary other 
than to do this.

I want to do something like this

DynaActionForm theForm = (DynaActionForm) form;
jspContext = ...

Map map = BeanUtils.describe(theForm);
jspContext.setAttribute("email",map);

//and now request the page and stream it.
.. ???

My nose is firmly lodged in the servlet and struts api's, just hoping 
someone can help nudge me in the right direction.






---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: JSP context in an action

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
Yeah, RequestDsiapatcher would work (like the struts IncludeAction) with the
StoreTag I sent, then you wouldn't have to forward in the jsp.

-------Action.execute()-------
// Determine Email Template
Strint emailTemplate = "EmailTemplate.jsp";

// Get formatted email content
javax.servlet.RequestDispatcher rd =
servlet.getServletContext().getRequestDispatcher(emailTemplate);
rd.include(request, response);
String emailContent = form.getEmailContent();

// Process
....send email---

 ------EmailTemplate.jsp-----
 <custom:store name="myForm" property="emailContent" scope="session">
     ------ format your email here -----
 </custom:store>


...and use the StoreTag to just save the contents of the jsp in a
bean/request/session

Niall

P.S. The first line in the doAfterBody() was a mistake in the StoreTag.


----- Original Message ----- 
From: "Mark Lowe" <ma...@talk21.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Tuesday, March 02, 2004 2:02 PM
Subject: Re: JSP context in an action


> I'm still chewing on the problem. But a custom tag lib would seem a
> possibility. I ended up using a velocity servlet and doing things that
> way, my aim in trying otherwise was not to have to use velocity as its
> another bunch of libraries that in theory I shouldn't need.
>
> Ideally i'd have a template directory under WEB-INF where numb-nuts
> dreamweaver types can edit email templates and i particularly like the
> expression syntax which is like jsp2. So i was trying to think of a way
> of using just that.
>
> Velocity would be a really tidy way of doing things if the
> VelocityEngine class had a getTemplate method that takes in a file
> rather than just a string. At the moment I've a velocity servlet thats
> really only there to get around this problem.
>
> But as things are its not that bad i've have just preferred not having
> to have a velocity servlet running just to find the where abouts of the
> template.
>
> Using jsp even better as everythings already there, just how to drill
> to what i need. RequestDispatcher could be an option request the file
> get its context and then stuff the map in there, but could be
> tree-barking or/and smoking too much crack.
>
>
>
> On 2 Mar 2004, at 12:59, Niall Pemberton wrote:
>
> > Mark,
> >
> > I'd like to know how to do what you're but, unless someone else knows,
> > how
> > about a different approach:
> >
> > You could have a "store" tag which gets the body of a tag and stores it
> > somewhere (in the request or session or in a bean in the request or
> > session)
> > and then forwards to an email action which then gets the stored
> > content and
> > sends an email. Something like:
> >
> > ------EmailTemplate.jsp-----
> > <custom:store name="myForm" property="emailContent" scope="session">
> >     ------ format your email here -----
> > </custom:store>
> > <logic:forward name="emailForward"/>
> >
> >
> > ------- StoreTag.java---------
> >
> > package lib.framework.taglib;
> > import javax.servlet.jsp.tagext.BodyTagSupport;
> > import javax.servlet.jsp.JspException;
> > import org.apache.struts.util.RequestUtils;
> > import org.apache.commons.beanutils.BeanUtils;
> > /**
> > * @author Niall Pemberton
> > * @version 1.0.0
> > */
> > public class StoreTag extends BodyTagSupport {
> >
> > protected String name = null;
> > protected String property = null;
> > protected String scope = null;
> >
> > public StoreTag() {
> > }
> > public void setName(String name) {
> > this.name = name;
> > }
> > public String getName() {
> > return name;
> > }
> > public void setProperty(String property) {
> > this.property = property;
> > }
> > public String getProperty() {
> > return property;
> > }
> > public void setScope(String scope) {
> > this.scope = scope;
> > }
> > public String getScope() {
> > return scope;
> > }
> > public int doAfterBody() throws JspException {
> > org.apache.struts.action.RequestProcessor ccc;
> > String value = null;
> > if (bodyContent != null) {
> > value = bodyContent.getString().trim();
> > }
> > if (property == null) {
> > pageContext.setAttribute(name, value, RequestUtils.getScope(scope));
> > } else {
> > // find the bean
> > Object bean = RequestUtils.lookup(pageContext, name, scope);
> > if (bean == null)
> > throw new JspException("Cannot find bean '"+name+"' in scope
> > '"+scope+"'");
> > try {
> > BeanUtils.setProperty(bean, property, value);
> > }
> > catch (Exception ex) {
> > throw new JspException("Error setting property '"+property+"' on bean
> > '"+name+"' in scope '"+scope+"': "+ex);
> > }
> > }
> > return (SKIP_BODY);
> > }
> > public void release() {
> > name = null;
> > property = null;
> > scope = null;
> > }
> > }
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: struts-user-help@jakarta.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: JSP context in an action

Posted by Mark Lowe <ma...@talk21.com>.
I'm still chewing on the problem. But a custom tag lib would seem a 
possibility. I ended up using a velocity servlet and doing things that 
way, my aim in trying otherwise was not to have to use velocity as its 
another bunch of libraries that in theory I shouldn't need.

Ideally i'd have a template directory under WEB-INF where numb-nuts 
dreamweaver types can edit email templates and i particularly like the 
expression syntax which is like jsp2. So i was trying to think of a way 
of using just that.

Velocity would be a really tidy way of doing things if the 
VelocityEngine class had a getTemplate method that takes in a file 
rather than just a string. At the moment I've a velocity servlet thats 
really only there to get around this problem.

But as things are its not that bad i've have just preferred not having 
to have a velocity servlet running just to find the where abouts of the 
template.

Using jsp even better as everythings already there, just how to drill 
to what i need. RequestDispatcher could be an option request the file 
get its context and then stuff the map in there, but could be 
tree-barking or/and smoking too much crack.



On 2 Mar 2004, at 12:59, Niall Pemberton wrote:

> Mark,
>
> I'd like to know how to do what you're but, unless someone else knows, 
> how
> about a different approach:
>
> You could have a "store" tag which gets the body of a tag and stores it
> somewhere (in the request or session or in a bean in the request or 
> session)
> and then forwards to an email action which then gets the stored 
> content and
> sends an email. Something like:
>
> ------EmailTemplate.jsp-----
> <custom:store name="myForm" property="emailContent" scope="session">
>     ------ format your email here -----
> </custom:store>
> <logic:forward name="emailForward"/>
>
>
> ------- StoreTag.java---------
>
> package lib.framework.taglib;
> import javax.servlet.jsp.tagext.BodyTagSupport;
> import javax.servlet.jsp.JspException;
> import org.apache.struts.util.RequestUtils;
> import org.apache.commons.beanutils.BeanUtils;
> /**
> * @author Niall Pemberton
> * @version 1.0.0
> */
> public class StoreTag extends BodyTagSupport {
>
> protected String name = null;
> protected String property = null;
> protected String scope = null;
>
> public StoreTag() {
> }
> public void setName(String name) {
> this.name = name;
> }
> public String getName() {
> return name;
> }
> public void setProperty(String property) {
> this.property = property;
> }
> public String getProperty() {
> return property;
> }
> public void setScope(String scope) {
> this.scope = scope;
> }
> public String getScope() {
> return scope;
> }
> public int doAfterBody() throws JspException {
> org.apache.struts.action.RequestProcessor ccc;
> String value = null;
> if (bodyContent != null) {
> value = bodyContent.getString().trim();
> }
> if (property == null) {
> pageContext.setAttribute(name, value, RequestUtils.getScope(scope));
> } else {
> // find the bean
> Object bean = RequestUtils.lookup(pageContext, name, scope);
> if (bean == null)
> throw new JspException("Cannot find bean '"+name+"' in scope 
> '"+scope+"'");
> try {
> BeanUtils.setProperty(bean, property, value);
> }
> catch (Exception ex) {
> throw new JspException("Error setting property '"+property+"' on bean
> '"+name+"' in scope '"+scope+"': "+ex);
> }
> }
> return (SKIP_BODY);
> }
> public void release() {
> name = null;
> property = null;
> scope = null;
> }
> }
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: JSP context in an action

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
Mark,

I'd like to know how to do what you're but, unless someone else knows, how
about a different approach:

You could have a "store" tag which gets the body of a tag and stores it
somewhere (in the request or session or in a bean in the request or session)
and then forwards to an email action which then gets the stored content and
sends an email. Something like:

------EmailTemplate.jsp-----
<custom:store name="myForm" property="emailContent" scope="session">
    ------ format your email here -----
</custom:store>
<logic:forward name="emailForward"/>


------- StoreTag.java---------

package lib.framework.taglib;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.JspException;
import org.apache.struts.util.RequestUtils;
import org.apache.commons.beanutils.BeanUtils;
/**
* @author Niall Pemberton
* @version 1.0.0
*/
public class StoreTag extends BodyTagSupport {

protected String name = null;
protected String property = null;
protected String scope = null;

public StoreTag() {
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setProperty(String property) {
this.property = property;
}
public String getProperty() {
return property;
}
public void setScope(String scope) {
this.scope = scope;
}
public String getScope() {
return scope;
}
public int doAfterBody() throws JspException {
org.apache.struts.action.RequestProcessor ccc;
String value = null;
if (bodyContent != null) {
value = bodyContent.getString().trim();
}
if (property == null) {
pageContext.setAttribute(name, value, RequestUtils.getScope(scope));
} else {
// find the bean
Object bean = RequestUtils.lookup(pageContext, name, scope);
if (bean == null)
throw new JspException("Cannot find bean '"+name+"' in scope '"+scope+"'");
try {
BeanUtils.setProperty(bean, property, value);
}
catch (Exception ex) {
throw new JspException("Error setting property '"+property+"' on bean
'"+name+"' in scope '"+scope+"': "+ex);
}
}
return (SKIP_BODY);
}
public void release() {
name = null;
property = null;
scope = null;
}
}



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org