Output Link and Command link problem

I have this command link that I want to redirect to a certain url, a complete one with the Http://www.webpage.com.. and its value is Goto This Page..

lets say I want it to pass through a certain backing bean before redirecting how am i supposed to do it...?

before it was this simple:

<h:outputLink value="http://www.wepbage.com">

<h:outputText value="Go to this page"/>

</h:outputLink>

but this could says that the action attribute is not valid for an outputLink..

thanks in advance.. :)

[597 byte] By [Arch_Bytesa] at [2007-11-27 11:01:44]
# 1

JSF<h:commandLink value="Go to this page" action="#{myBean.link}" />

MyBeanpublic void link() {

HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();

response.sendRedirect("http://www.webpage.com");

}

BalusCa at 2007-7-29 12:38:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

But how about if I want it to do something before passing through that link? lets say a different backing bean..?

Do I have to add an action listener to it?

Arch_Bytesa at 2007-7-29 12:38:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Uhm, just extend the link() method with desired logic?

This was just a basic example.

BalusCa at 2007-7-29 12:38:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Cannot forward after response has been committed--

this is what I got when I extended the desired logic into the redirect.. i placed a method before the redirect..

Arch_Bytesa at 2007-7-29 12:38:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

You cannot send multiple responses at once.

Are you returning a navigation case String afterwards?

BalusCa at 2007-7-29 12:38:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

oh.. ok.. but I need to do something before it should redirect. I am simply supposed to set a boolean value into true but then as you said I can't do that.. I am not returning navigation strings...

is there another way around this..?

Arch_Bytesa at 2007-7-29 12:38:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

This is just possible. public void link() {

someBooleanValue = true;

response.sendRedirect("foo");

}

Look to the stacktrace what's the cause of the IllegalStateException.

BalusCa at 2007-7-29 12:38:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

Here's the stack trace but i can't seem to find which particular thing is causing this..

16:30:33,921 ERROR [[Faces Servlet]] Servlet.service() for servlet Faces Servlet

threw exception

java.lang.IllegalStateException: Cannot forward after response has been committe

d

at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationD

ispatcher.java:313)

at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDis

patcher.java:301)

at org.apache.myfaces.context.servlet.ServletExternalContextImpl.dispatc

h(ServletExternalContextImpl.java:415)

at org.apache.myfaces.application.jsp.JspViewHandlerImpl.renderView(JspV

iewHandlerImpl.java:234)

at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:

352)

at javax.faces.webapp.FacesServlet.service(FacesServlet.java:107)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl

icationFilterChain.java:252)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF

ilterChain.java:173)

at org.apache.myfaces.component.html.util.ExtensionsFilter.doFilter(Exte

nsionsFilter.java:92)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl

icationFilterChain.java:202)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF

ilterChain.java:173)

at org.apache.myfaces.component.html.util.ExtensionsFilter.doFilter(Exte

nsionsFilter.java:92)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl

icationFilterChain.java:202)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF

ilterChain.java:173)

at org.apache.myfaces.component.html.util.ExtensionsFilter.doFilter(Exte

nsionsFilter.java:122)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl

icationFilterChain.java:202)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF

ilterChain.java:173)

at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFi

lter.java:96)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl

icationFilterChain.java:202)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF

ilterChain.java:173)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV

alve.java:213)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV

alve.java:178)

at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(Securit

yAssociationValve.java:175)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(Authentica

torBase.java:432)

at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValv

e.java:74)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j

ava:126)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j

ava:105)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal

ve.java:107)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav

a:148)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java

:869)

at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.p

rocessConnection(Http11BaseProtocol.java:664)

at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpo

int.java:527)

at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWor

kerThread.java:112)

at java.lang.Thread.run(Thread.java:595)

here's the bean

public void link(){

HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();

try {

searchController.setNewSearch(true);

response.sendRedirect("http://www.shipserv.com");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

Arch_Bytesa at 2007-7-29 12:38:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

i can't even redirect it plainly its still gives me the error Cannot forward after response has been committed..

<h:commandLink value="Go to this page" action="#{supplierController.link}"/>

my backing bean:

public void link(){

HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext();

try {

response.sendRedirect("http://www.shipserv.com");

} catch (IOException e) {

e.printStackTrace();

}

}

desperately in need of help..

Arch_Bytesa at 2007-7-29 12:38:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

AddFacesContext.getCurrentInstance().responseComplete();

after the sendRedirect.

BalusCa at 2007-7-29 12:38:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

thanks balus! it finally worked.. :)

Arch_Bytesa at 2007-7-29 12:38:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...