You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Raghuveer Rawat <te...@gmail.com> on 2008/01/21 20:25:27 UTC

[Struts2] Ajax anchor tag problem

Hi, I have a requirement where only logged in users are allowed to put their
comment. I have implemented that form with struts2 ajax theme...anchor has
theme="ajax"

If logged in user  puts his comment then  everything  works fine
perfectly..I am able to see text message returned from action in target
div....

But when a user who is not logged in and try to put this comment..I want to
redirect user to login page..*but there what is happening here is that login
page is also streaming back and it is opening in target div..*

Any idea how to redirect user to login page..

Below is my action class method..

public String addComment() {
        try{
            UserTO userTo = null;
            Object user = this.getSession().get("user");
            if(user == null){
                return Action.LOGIN;
            }else{
                userTo = (UserTO) user;
            }
            this.getCommentTo().setUserId(userTo.getUserId());
            this.getCommentTo().setArticleId(this.getArticleId());

            this.getCommentService().addComment(this.getCommentTo());
            HttpServletResponse response = ServletActionContext.getResponse
();
            //inputStream = new StringBufferInputStream("Comment Posted
Successfully.");
            String successMsg = "Thanks for posting comment. Your comment
has been added successfully.";
            byte[] contents = successMsg.getBytes();
            response.getOutputStream().write(contents);

        }catch(Exception ex){
            LOG.error("Exception in myArticles():"+ ex);
            return Action.ERROR;
        }
        return null;
    }


This is my comment form...

               <div id='commentResponse'>
                </div>
            <s:form action="comments_addComment"  method="post" >


            <table width="583" border="0" cellspacing="2" cellpadding="2">
                <tr>
                  <td width="99" align="left"><strong>Your Comments</strong>
                              </td>
                  <td width="470" align="left">
                    <s:textarea name="commentTo.comments"  rows="5"
cols="40"></s:textarea>
                    <s:hidden name="articleId" value="${article.articleId
}"/></td>
                </tr>
                <tr>
                  <td align="left">&nbsp;</td>
                  <td width="470" align="left">

                  <s:a theme="ajax" targets="commentResponse">Add
Comment</s:a>
                  </td>
                </tr>
              </table>
            </s:form>

Below is struts.xml entry..I am using tiles and default results are tile
definitions....I also tried using redirect-action and redirect but that also
give same problem..

         <action name="comments_*" method="{1}" class="
com.rawatsoft.write4smile.webapp.action.CommentAction">
            <result name="login">login</result>

        </action>

Re: [Struts2] Ajax anchor tag problem

Posted by Raghuveer Rawat <te...@gmail.com>.
Thanks Wong, yeah, I will try to implement your suggestion..

On Jan 22, 2008 12:07 AM, WongTseng <wa...@gmail.com> wrote:

> since the request is sent via ajax, so the browser side redirect can
> not work. i suggest you don't redirect you user directly, instead you
> send back a piece of js, let the js redirect the browser.
> <script>
> window.location='xxx';
> </script>
>
> 2008/1/21, Raghuveer Rawat <te...@gmail.com>:
> > Thanks Robi, I think you are right but how to come out of this ajax call
> if
> > something goes wrong.....sometimes I am getting some technical error and
> > error page is also opening under target div.
> > Is there anyone who faced this issue early?
> >
> >
> > On Jan 21, 2008 3:29 PM, Roberto Nunnari <ro...@supsi.ch>
> wrote:
> >
> > > oopss.. sorry.. I read your post to fast and didn't notice you already
> > > tried redirect-action..
> > >
> > > I believe the problem is that it's not the browser who is getting the
> > > redirect message from the server, but the ajax async call system, and
> so
> > > it puts the login form in the div, as it was instructed.. but as I'm
> > > not an ajax guru, I could be wrong.
> > >
> > > Best regards.
> > >
> > > --
> > > Robi
> > >
> > >
> > > Roberto Nunnari wrote:
> > > > try this:
> > > >
> > > > <result name="login" type="redirect-action">login</result>
> > > >
> > > > Best regards.
> > > >
> > > > --
> > > > Robi
> > > >
> > > >
> > > > Raghuveer Rawat wrote:
> > > >> Hi, I have a requirement where only logged in users are allowed to
> put
> > > >> their
> > > >> comment. I have implemented that form with struts2 ajax
> theme...anchor
> > > >> has
> > > >> theme="ajax"
> > > >>
> > > >> If logged in user  puts his comment then  everything  works fine
> > > >> perfectly..I am able to see text message returned from action in
> target
> > > >> div....
> > > >>
> > > >> But when a user who is not logged in and try to put this comment..I
> > > >> want to
> > > >> redirect user to login page..*but there what is happening here is
> that
> > > >> login
> > > >> page is also streaming back and it is opening in target div..*
> > > >>
> > > >> Any idea how to redirect user to login page..
> > > >>
> > > >> Below is my action class method..
> > > >>
> > > >> public String addComment() {
> > > >>         try{
> > > >>             UserTO userTo = null;
> > > >>             Object user = this.getSession().get("user");
> > > >>             if(user == null){
> > > >>                 return Action.LOGIN;
> > > >>             }else{
> > > >>                 userTo = (UserTO) user;
> > > >>             }
> > > >>             this.getCommentTo().setUserId(userTo.getUserId());
> > > >>             this.getCommentTo().setArticleId(this.getArticleId());
> > > >>
> > > >>             this.getCommentService().addComment(this.getCommentTo
> ());
> > > >>             HttpServletResponse response =
> > > >> ServletActionContext.getResponse
> > > >> ();
> > > >>             //inputStream = new StringBufferInputStream("Comment
> Posted
> > > >> Successfully.");
> > > >>             String successMsg = "Thanks for posting comment. Your
> > > comment
> > > >> has been added successfully.";
> > > >>             byte[] contents = successMsg.getBytes();
> > > >>             response.getOutputStream().write(contents);
> > > >>
> > > >>         }catch(Exception ex){
> > > >>             LOG.error("Exception in myArticles():"+ ex);
> > > >>             return Action.ERROR;
> > > >>         }
> > > >>         return null;
> > > >>     }
> > > >>
> > > >>
> > > >> This is my comment form...
> > > >>
> > > >>                <div id='commentResponse'>
> > > >>                 </div>
> > > >>             <s:form action="comments_addComment"  method="post" >
> > > >>
> > > >>
> > > >>             <table width="583" border="0" cellspacing="2"
> > > >> cellpadding="2">
> > > >>                 <tr>
> > > >>                   <td width="99" align="left"><strong>Your
> > > >> Comments</strong>
> > > >>                               </td>
> > > >>                   <td width="470" align="left">
> > > >>                     <s:textarea name="commentTo.comments"  rows="5"
> > > >> cols="40"></s:textarea>
> > > >>                     <s:hidden name="articleId" value="${
> > > article.articleId
> > > >> }"/></td>
> > > >>                 </tr>
> > > >>                 <tr>
> > > >>                   <td align="left">&nbsp;</td>
> > > >>                   <td width="470" align="left">
> > > >>
> > > >>                   <s:a theme="ajax" targets="commentResponse">Add
> > > >> Comment</s:a>
> > > >>                   </td>
> > > >>                 </tr>
> > > >>               </table>
> > > >>             </s:form>
> > > >>
> > > >> Below is struts.xml entry..I am using tiles and default results are
> > > tile
> > > >> definitions....I also tried using redirect-action and redirect but
> > > >> that also
> > > >> give same problem..
> > > >>
> > > >>          <action name="comments_*" method="{1}" class="
> > > >> com.rawatsoft.write4smile.webapp.action.CommentAction">
> > > >>             <result name="login">login</result>
> > > >>
> > > >>         </action>
> > > >>
> > > >
> > > >
> > > >
> ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > > > For additional commands, e-mail: user-help@struts.apache.org
> > > >
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > > For additional commands, e-mail: user-help@struts.apache.org
> > >
> > >
> >
>
>
> --
> Best Regards
> Wong Tseng
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: [Struts2] Ajax anchor tag problem

Posted by WongTseng <wa...@gmail.com>.
since the request is sent via ajax, so the browser side redirect can
not work. i suggest you don't redirect you user directly, instead you
send back a piece of js, let the js redirect the browser.
<script>
window.location='xxx';
</script>

2008/1/21, Raghuveer Rawat <te...@gmail.com>:
> Thanks Robi, I think you are right but how to come out of this ajax call if
> something goes wrong.....sometimes I am getting some technical error and
> error page is also opening under target div.
> Is there anyone who faced this issue early?
>
>
> On Jan 21, 2008 3:29 PM, Roberto Nunnari <ro...@supsi.ch> wrote:
>
> > oopss.. sorry.. I read your post to fast and didn't notice you already
> > tried redirect-action..
> >
> > I believe the problem is that it's not the browser who is getting the
> > redirect message from the server, but the ajax async call system, and so
> > it puts the login form in the div, as it was instructed.. but as I'm
> > not an ajax guru, I could be wrong.
> >
> > Best regards.
> >
> > --
> > Robi
> >
> >
> > Roberto Nunnari wrote:
> > > try this:
> > >
> > > <result name="login" type="redirect-action">login</result>
> > >
> > > Best regards.
> > >
> > > --
> > > Robi
> > >
> > >
> > > Raghuveer Rawat wrote:
> > >> Hi, I have a requirement where only logged in users are allowed to put
> > >> their
> > >> comment. I have implemented that form with struts2 ajax theme...anchor
> > >> has
> > >> theme="ajax"
> > >>
> > >> If logged in user  puts his comment then  everything  works fine
> > >> perfectly..I am able to see text message returned from action in target
> > >> div....
> > >>
> > >> But when a user who is not logged in and try to put this comment..I
> > >> want to
> > >> redirect user to login page..*but there what is happening here is that
> > >> login
> > >> page is also streaming back and it is opening in target div..*
> > >>
> > >> Any idea how to redirect user to login page..
> > >>
> > >> Below is my action class method..
> > >>
> > >> public String addComment() {
> > >>         try{
> > >>             UserTO userTo = null;
> > >>             Object user = this.getSession().get("user");
> > >>             if(user == null){
> > >>                 return Action.LOGIN;
> > >>             }else{
> > >>                 userTo = (UserTO) user;
> > >>             }
> > >>             this.getCommentTo().setUserId(userTo.getUserId());
> > >>             this.getCommentTo().setArticleId(this.getArticleId());
> > >>
> > >>             this.getCommentService().addComment(this.getCommentTo());
> > >>             HttpServletResponse response =
> > >> ServletActionContext.getResponse
> > >> ();
> > >>             //inputStream = new StringBufferInputStream("Comment Posted
> > >> Successfully.");
> > >>             String successMsg = "Thanks for posting comment. Your
> > comment
> > >> has been added successfully.";
> > >>             byte[] contents = successMsg.getBytes();
> > >>             response.getOutputStream().write(contents);
> > >>
> > >>         }catch(Exception ex){
> > >>             LOG.error("Exception in myArticles():"+ ex);
> > >>             return Action.ERROR;
> > >>         }
> > >>         return null;
> > >>     }
> > >>
> > >>
> > >> This is my comment form...
> > >>
> > >>                <div id='commentResponse'>
> > >>                 </div>
> > >>             <s:form action="comments_addComment"  method="post" >
> > >>
> > >>
> > >>             <table width="583" border="0" cellspacing="2"
> > >> cellpadding="2">
> > >>                 <tr>
> > >>                   <td width="99" align="left"><strong>Your
> > >> Comments</strong>
> > >>                               </td>
> > >>                   <td width="470" align="left">
> > >>                     <s:textarea name="commentTo.comments"  rows="5"
> > >> cols="40"></s:textarea>
> > >>                     <s:hidden name="articleId" value="${
> > article.articleId
> > >> }"/></td>
> > >>                 </tr>
> > >>                 <tr>
> > >>                   <td align="left">&nbsp;</td>
> > >>                   <td width="470" align="left">
> > >>
> > >>                   <s:a theme="ajax" targets="commentResponse">Add
> > >> Comment</s:a>
> > >>                   </td>
> > >>                 </tr>
> > >>               </table>
> > >>             </s:form>
> > >>
> > >> Below is struts.xml entry..I am using tiles and default results are
> > tile
> > >> definitions....I also tried using redirect-action and redirect but
> > >> that also
> > >> give same problem..
> > >>
> > >>          <action name="comments_*" method="{1}" class="
> > >> com.rawatsoft.write4smile.webapp.action.CommentAction">
> > >>             <result name="login">login</result>
> > >>
> > >>         </action>
> > >>
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > > For additional commands, e-mail: user-help@struts.apache.org
> > >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>


-- 
Best Regards
Wong Tseng

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


Re: [Struts2] Ajax anchor tag problem

Posted by Raghuveer Rawat <te...@gmail.com>.
Thanks Robi, I think you are right but how to come out of this ajax call if
something goes wrong.....sometimes I am getting some technical error and
error page is also opening under target div.
Is there anyone who faced this issue early?


On Jan 21, 2008 3:29 PM, Roberto Nunnari <ro...@supsi.ch> wrote:

> oopss.. sorry.. I read your post to fast and didn't notice you already
> tried redirect-action..
>
> I believe the problem is that it's not the browser who is getting the
> redirect message from the server, but the ajax async call system, and so
> it puts the login form in the div, as it was instructed.. but as I'm
> not an ajax guru, I could be wrong.
>
> Best regards.
>
> --
> Robi
>
>
> Roberto Nunnari wrote:
> > try this:
> >
> > <result name="login" type="redirect-action">login</result>
> >
> > Best regards.
> >
> > --
> > Robi
> >
> >
> > Raghuveer Rawat wrote:
> >> Hi, I have a requirement where only logged in users are allowed to put
> >> their
> >> comment. I have implemented that form with struts2 ajax theme...anchor
> >> has
> >> theme="ajax"
> >>
> >> If logged in user  puts his comment then  everything  works fine
> >> perfectly..I am able to see text message returned from action in target
> >> div....
> >>
> >> But when a user who is not logged in and try to put this comment..I
> >> want to
> >> redirect user to login page..*but there what is happening here is that
> >> login
> >> page is also streaming back and it is opening in target div..*
> >>
> >> Any idea how to redirect user to login page..
> >>
> >> Below is my action class method..
> >>
> >> public String addComment() {
> >>         try{
> >>             UserTO userTo = null;
> >>             Object user = this.getSession().get("user");
> >>             if(user == null){
> >>                 return Action.LOGIN;
> >>             }else{
> >>                 userTo = (UserTO) user;
> >>             }
> >>             this.getCommentTo().setUserId(userTo.getUserId());
> >>             this.getCommentTo().setArticleId(this.getArticleId());
> >>
> >>             this.getCommentService().addComment(this.getCommentTo());
> >>             HttpServletResponse response =
> >> ServletActionContext.getResponse
> >> ();
> >>             //inputStream = new StringBufferInputStream("Comment Posted
> >> Successfully.");
> >>             String successMsg = "Thanks for posting comment. Your
> comment
> >> has been added successfully.";
> >>             byte[] contents = successMsg.getBytes();
> >>             response.getOutputStream().write(contents);
> >>
> >>         }catch(Exception ex){
> >>             LOG.error("Exception in myArticles():"+ ex);
> >>             return Action.ERROR;
> >>         }
> >>         return null;
> >>     }
> >>
> >>
> >> This is my comment form...
> >>
> >>                <div id='commentResponse'>
> >>                 </div>
> >>             <s:form action="comments_addComment"  method="post" >
> >>
> >>
> >>             <table width="583" border="0" cellspacing="2"
> >> cellpadding="2">
> >>                 <tr>
> >>                   <td width="99" align="left"><strong>Your
> >> Comments</strong>
> >>                               </td>
> >>                   <td width="470" align="left">
> >>                     <s:textarea name="commentTo.comments"  rows="5"
> >> cols="40"></s:textarea>
> >>                     <s:hidden name="articleId" value="${
> article.articleId
> >> }"/></td>
> >>                 </tr>
> >>                 <tr>
> >>                   <td align="left">&nbsp;</td>
> >>                   <td width="470" align="left">
> >>
> >>                   <s:a theme="ajax" targets="commentResponse">Add
> >> Comment</s:a>
> >>                   </td>
> >>                 </tr>
> >>               </table>
> >>             </s:form>
> >>
> >> Below is struts.xml entry..I am using tiles and default results are
> tile
> >> definitions....I also tried using redirect-action and redirect but
> >> that also
> >> give same problem..
> >>
> >>          <action name="comments_*" method="{1}" class="
> >> com.rawatsoft.write4smile.webapp.action.CommentAction">
> >>             <result name="login">login</result>
> >>
> >>         </action>
> >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: [Struts2] Ajax anchor tag problem

Posted by Roberto Nunnari <ro...@supsi.ch>.
oopss.. sorry.. I read your post to fast and didn't notice you already
tried redirect-action..

I believe the problem is that it's not the browser who is getting the
redirect message from the server, but the ajax async call system, and so
it puts the login form in the div, as it was instructed.. but as I'm
not an ajax guru, I could be wrong.

Best regards.

--
Robi


Roberto Nunnari wrote:
> try this:
> 
> <result name="login" type="redirect-action">login</result>
> 
> Best regards.
> 
> -- 
> Robi
> 
> 
> Raghuveer Rawat wrote:
>> Hi, I have a requirement where only logged in users are allowed to put 
>> their
>> comment. I have implemented that form with struts2 ajax theme...anchor 
>> has
>> theme="ajax"
>>
>> If logged in user  puts his comment then  everything  works fine
>> perfectly..I am able to see text message returned from action in target
>> div....
>>
>> But when a user who is not logged in and try to put this comment..I 
>> want to
>> redirect user to login page..*but there what is happening here is that 
>> login
>> page is also streaming back and it is opening in target div..*
>>
>> Any idea how to redirect user to login page..
>>
>> Below is my action class method..
>>
>> public String addComment() {
>>         try{
>>             UserTO userTo = null;
>>             Object user = this.getSession().get("user");
>>             if(user == null){
>>                 return Action.LOGIN;
>>             }else{
>>                 userTo = (UserTO) user;
>>             }
>>             this.getCommentTo().setUserId(userTo.getUserId());
>>             this.getCommentTo().setArticleId(this.getArticleId());
>>
>>             this.getCommentService().addComment(this.getCommentTo());
>>             HttpServletResponse response = 
>> ServletActionContext.getResponse
>> ();
>>             //inputStream = new StringBufferInputStream("Comment Posted
>> Successfully.");
>>             String successMsg = "Thanks for posting comment. Your comment
>> has been added successfully.";
>>             byte[] contents = successMsg.getBytes();
>>             response.getOutputStream().write(contents);
>>
>>         }catch(Exception ex){
>>             LOG.error("Exception in myArticles():"+ ex);
>>             return Action.ERROR;
>>         }
>>         return null;
>>     }
>>
>>
>> This is my comment form...
>>
>>                <div id='commentResponse'>
>>                 </div>
>>             <s:form action="comments_addComment"  method="post" >
>>
>>
>>             <table width="583" border="0" cellspacing="2" 
>> cellpadding="2">
>>                 <tr>
>>                   <td width="99" align="left"><strong>Your 
>> Comments</strong>
>>                               </td>
>>                   <td width="470" align="left">
>>                     <s:textarea name="commentTo.comments"  rows="5"
>> cols="40"></s:textarea>
>>                     <s:hidden name="articleId" value="${article.articleId
>> }"/></td>
>>                 </tr>
>>                 <tr>
>>                   <td align="left">&nbsp;</td>
>>                   <td width="470" align="left">
>>
>>                   <s:a theme="ajax" targets="commentResponse">Add
>> Comment</s:a>
>>                   </td>
>>                 </tr>
>>               </table>
>>             </s:form>
>>
>> Below is struts.xml entry..I am using tiles and default results are tile
>> definitions....I also tried using redirect-action and redirect but 
>> that also
>> give same problem..
>>
>>          <action name="comments_*" method="{1}" class="
>> com.rawatsoft.write4smile.webapp.action.CommentAction">
>>             <result name="login">login</result>
>>
>>         </action>
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 


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


Re: [Struts2] Ajax anchor tag problem

Posted by Roberto Nunnari <ro...@supsi.ch>.
try this:

<result name="login" type="redirect-action">login</result>

Best regards.

--
Robi


Raghuveer Rawat wrote:
> Hi, I have a requirement where only logged in users are allowed to put their
> comment. I have implemented that form with struts2 ajax theme...anchor has
> theme="ajax"
> 
> If logged in user  puts his comment then  everything  works fine
> perfectly..I am able to see text message returned from action in target
> div....
> 
> But when a user who is not logged in and try to put this comment..I want to
> redirect user to login page..*but there what is happening here is that login
> page is also streaming back and it is opening in target div..*
> 
> Any idea how to redirect user to login page..
> 
> Below is my action class method..
> 
> public String addComment() {
>         try{
>             UserTO userTo = null;
>             Object user = this.getSession().get("user");
>             if(user == null){
>                 return Action.LOGIN;
>             }else{
>                 userTo = (UserTO) user;
>             }
>             this.getCommentTo().setUserId(userTo.getUserId());
>             this.getCommentTo().setArticleId(this.getArticleId());
> 
>             this.getCommentService().addComment(this.getCommentTo());
>             HttpServletResponse response = ServletActionContext.getResponse
> ();
>             //inputStream = new StringBufferInputStream("Comment Posted
> Successfully.");
>             String successMsg = "Thanks for posting comment. Your comment
> has been added successfully.";
>             byte[] contents = successMsg.getBytes();
>             response.getOutputStream().write(contents);
> 
>         }catch(Exception ex){
>             LOG.error("Exception in myArticles():"+ ex);
>             return Action.ERROR;
>         }
>         return null;
>     }
> 
> 
> This is my comment form...
> 
>                <div id='commentResponse'>
>                 </div>
>             <s:form action="comments_addComment"  method="post" >
> 
> 
>             <table width="583" border="0" cellspacing="2" cellpadding="2">
>                 <tr>
>                   <td width="99" align="left"><strong>Your Comments</strong>
>                               </td>
>                   <td width="470" align="left">
>                     <s:textarea name="commentTo.comments"  rows="5"
> cols="40"></s:textarea>
>                     <s:hidden name="articleId" value="${article.articleId
> }"/></td>
>                 </tr>
>                 <tr>
>                   <td align="left">&nbsp;</td>
>                   <td width="470" align="left">
> 
>                   <s:a theme="ajax" targets="commentResponse">Add
> Comment</s:a>
>                   </td>
>                 </tr>
>               </table>
>             </s:form>
> 
> Below is struts.xml entry..I am using tiles and default results are tile
> definitions....I also tried using redirect-action and redirect but that also
> give same problem..
> 
>          <action name="comments_*" method="{1}" class="
> com.rawatsoft.write4smile.webapp.action.CommentAction">
>             <result name="login">login</result>
> 
>         </action>
> 


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