users@jersey.java.net

[Jersey] Re: Forward to Servlet

From: cmdotmani <cmdotmani_at_gmail.com>
Date: Thu, 16 Dec 2010 17:12:30 -0800 (PST)

Hi Paul,

Thank you so much..
I created a ExceptionMapper & new Exception class that takes template name,
request object.

My RequestPojo will throw exception like below

if (StringUtils.isBlank(_email)) {
      throw new MYException(ErrorCodes.BAD_REQUEST.getError("Email Address
Required"),
        "/public/signup", this);
    }

I am also passing the request object so when my page is rendered i can
repopulate previously entered form values.

Thanks again.
Chandra


On Thu, Dec 16, 2010 at 6:48 AM, Paul Sandoz-2 [via Jersey] <
ml-node+5842144-2131725610-307074_at_n2.nabble.com<ml-node%2B5842144-2131725610-307074_at_n2.nabble.com>
> wrote:

> Hi,
>
> I am not entirely sure what you mean by:
>
> "do we have better way to catch the exception in same page"
>
> Do you want to use the same template to render both a success and an
> error?
>
> You can also throw a WebApplicationException with a response built
> using Viewable as an entity. Or use your own special exception that
> takes a viewable and use an exception mapper.
>
> You need to be explicit about the template reference, or provide a
> context that defines how the relative template reference is resolved
> to an absolute template reference. You can pass a class that defines
> such a context. That class can be a resource class and will obey the
> same rules as that for views returned normally.
>
> Hth,
> Paul.
>
> On Dec 16, 2010, at 1:13 AM, cmdotmani wrote:
>
> >
> > Hi Paul,
> >
> > Sorry I am may not be able to share my company code due to NDA :)
> > I tried the below and it worked perfectly.
> >
> > <servlet>
> > <servlet-name>MyWebApplication</servlet-name>
> > <servlet-class>com.myproj.servlet.UiResourceServlet</servlet-class>
> > <init-param>
> > <param-name>com.sun.jersey.config.feature.Redirect</param-name>
> > <param-value>true</param-value>
> > </init-param>
> > <init-param>
> >
> > <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</
> > param-name>
> > <param-value>/views/</param-value>
> > </init-param>
> > <init-param>
> >
> > <param-name>com.sun.jersey.config.property.WebPageContentRegex</
> > param-name>
> > <param-value>/(images|css|jsp)/.*</param-value>
> > </init-param>
> > </servlet>
> >
> > <servlet-mapping>
> > <servlet-name>MyWebApplication</servlet-name>
> > <url-pattern>/myproj/*</url-pattern>
> > </servlet-mapping>
> >
> >
> > @Component
> > @Path ("/myproj")
> > public class AdminUiResource extends AbstractResource {
> >
> > private static final Logger LOG =
> > LoggerFactory.getLogger(AdminUiResource.class);
> >
> > @GET
> > @Path("/signup")
> > @Produces("text/html")
> > public Viewable getSignUp() {
> > return new Viewable("/public/signup", "Test");
> > }
> >
> > --------------
> >
> > Now my question is while doing a POST like below, I want to throw
> > exception
> > from at request pojo
> >
> > @POST
> > @Path("/signup")
> > @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
> > public Response postSignUp(MyRequestObject request) throws
> > Exception {
> > ....
> >
> > "MyRequestObject" pojo does some validation and responses with error
> > message
> > which would look some thing like this...
> >
> > <error><code>0003</code><message>Incorrect request. [Email Address
> > invalid].</message></error>
> >
> > I saw your example from "Devoxx2010_JAX_RS_Samples" in which you
> > have some
> > thing like this..
> >
> > @Provider
> > public class RuntimeExceptionMapper implements
> > ExceptionMapper<RuntimeException> {
> > @Override
> > public Response toResponse(RuntimeException exception) {
> > return Response.status(500).
> > entity(new Viewable("/exception", exception)).
> > build();
> > }
> > }
> > which will call exception page with some scriplet in JSP page
> > <% Exception e = (Exception)request.getAttribute("it");
> > ..... %>
> >
> > do we have better way to catch the exception in same page. I can put
> > the
> > scriplet in my /signup page, but using scriplet sounds old way, is
> > there are
> > better approach ?
> >
> > See its just not about form validation errors, might be some other bad
> > request error.
> >
> > Thanks
> > Chandra
> > --
> > View this message in context:
> http://jersey.576304.n2.nabble.com/Forward-to-Servlet-tp5351731p5838644.html<http://jersey.576304.n2.nabble.com/Forward-to-Servlet-tp5351731p5838644.html?by-user=t>
>
> > Sent from the Jersey mailing list archive at Nabble.com.
>
>
>
> ------------------------------
> View message @
> http://jersey.576304.n2.nabble.com/Forward-to-Servlet-tp5351731p5842144.html
>
> To unsubscribe from Forward to Servlet, click here<http://jersey.576304.n2.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5351731&code=Y21kb3RtYW5pQGdtYWlsLmNvbXw1MzUxNzMxfC0xMzU0Mzg3MDEx>.
>
>

-- 
View this message in context: http://jersey.576304.n2.nabble.com/Forward-to-Servlet-tp5351731p5844148.html
Sent from the Jersey mailing list archive at Nabble.com.