Executar action ao ler uma página com webwork e velocity

1 resposta
abstract

Boa noite a todos, pesquisei no forum e não encontrei resposta se alguém souber dê uma luz, eu quero definir um index.vm por exemplo, e que quando a página central o index, for executada, lida, ela por exemplo já execute uma action do webwork, eu não consegui, definir o arquivo index.vm no meu web.xml, mas ele ao invés de retornar o valor do meu método get definido, retorna apenas $teste por exemplo, então consegui a solução menos viável de usar um javascript redirecionando pra essa página:

root@eureka:/opt/tomcat/webapps/tsw2/WebRoot# cat index.html
<script language="JavaScript">
document.location = "index.tsw";
</script>

Creio que exista solução pra isso mas já tentei olhar a documentação do webwork e do velocity e ainda não achei, abaixo seguem alguns dados:

meu web.xml está definido da seguinte forma:
<?xml version="1.0" encoding="UTF-8"?>
&lt;web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins
tance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
 &lt;description&gt;Configuração do xml&lt;/description&gt;
 &lt;display-name&gt;Teste&lt;/display-name&gt;
 &lt;servlet&gt;
   &lt;servlet-name&gt;webwork&lt;/servlet-name&gt;
   &lt;servlet-class&gt;com.opensymphony.webwork.dispatcher.ServletDispatcher&lt;/servlet-class&gt;
   &lt;init-param&gt;
     &lt;param-name&gt;&lt;load-on-startup&gt;&lt;/param-name&gt;
     &lt;param-value&gt;1&lt;/param-value&gt;
   &lt;/init-param&gt;
 &lt;/servlet&gt;
 &lt;servlet&gt;
   &lt;servlet-name&gt;velocity&lt;/servlet-name&gt;
   &lt;servlet-class&gt;com.opensymphony.webwork.views.velocity.WebWorkVelocityServlet&lt;/servlet-class&gt;
   &lt;init-param&gt;
     &lt;param-name&gt;&lt;load-on-startup&gt;&lt;/param-name&gt;
     &lt;param-value&gt;1&lt;/param-value&gt;
   &lt;/init-param&gt;
 &lt;/servlet&gt;
 &lt;servlet-mapping&gt;
   &lt;servlet-name&gt;webwork&lt;/servlet-name&gt;
   &lt;url-pattern&gt;*.tsw&lt;/url-pattern&gt;
 &lt;/servlet-mapping&gt;
 &lt;servlet-mapping&gt;
   &lt;servlet-name&gt;velocity&lt;/servlet-name&gt;
   &lt;url-pattern&gt;*.vm&lt;/url-pattern&gt;
 &lt;/servlet-mapping&gt;
 &lt;session-config&gt;
   &lt;session-timeout&gt;
           30
       &lt;/session-timeout&gt;
 &lt;/session-config&gt;
 &lt;welcome-file-list&gt;
   &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
 &lt;/welcome-file-list&gt;
&lt;/web-app&gt;
meu xwork.xml está da seguinte forma:
&lt;xwork&gt;
 &lt;include file="webwork-default.xml" /&gt;
 &lt;package name="default" extends="webwork-default"&gt;
   &lt;default-interceptor-ref name="defaultStack" /&gt;

   &lt;action name="index" class="com.tsw.Sessao2"&gt;
     &lt;result name="success" type="dispatcher"&gt;web/index.vm&lt;/result&gt;
   &lt;/action&gt;

 &lt;/package&gt;
&lt;/xwork&gt;

e o meu index.vm está assim:

&lt;html&gt;
&lt;body&gt;

$fechada

&lt;form action="index.tsw" method="POST"&gt;
<p>

Qual o seu nome? <br/>
&lt;input type="text" name="nome"/&gt;
&lt;input type="submit" value="Enviar"/&gt;
<p>
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

minha classe:

public class Sessao2 extends ActionSupport {
    
    private String fechada = "testando";
    
    public String execute() throws Exception {
        
        Class.forName(driver);
        Connection con = DriverManager.getConnection(url + banco, user, passwd);
        return SUCCESS;
        
    }
    
       
    public String getFechada() {
        
        return fechada;
    }
}

ps: Tentei por o index.vm dentro de web.xml mas ele não executa a action

1 Resposta

abstract

Sendo egocêntrico mais uma vez e respondendo a mim mesmo, lá vai a solução:

<action name=“index” class=“com.tsw.Sessao2”>

<result name=“success” type=“velocity”>web/index.vm</result>

</action>

o tipo do result tem que ser igual a velocity, já que o webwork o
implementa nativamente, sendo assim ele colocará o resultado
diretamente no template velocity, o dispatcher seria para encaminhar o
resultado para outro template, como descrito abaixo:

dispatcher (com.opensymphony.webwork.dispatcher.ServletDispatcherResult):
forwards the result to the specified location;

velocity (com.opensymphony.webwork.dispatcher.VelocityResult): uses a
Velocity template as the result. You could use the dispatcher to
forward results to Velocity pages if you have VelocityServlet
configured in web.xml, but using the Velocity result is a better
approach.

E depois removi do meu web.xml a tag xml que tinha antes, já que, ele
automaticamente encaminha pra minha action

Linha removida do web.xml

<welcome-file-list>
<welcome-file>web/index.vm</welcome-file>
</welcome-file-list>

Criado 29 de junho de 2005
Ultima resposta 30 de jun. de 2005
Respostas 1
Participantes 1