Olá Mundo com Struts2 {Problema na declaração da Anotação}[RESOLVIDO]

4 respostas
Thiago_de_Paula_Bese

Olá Gente.

Pessoal, to jogando pro NetBeans o que aprendi de Struts2. Comecei do ZERO {Ola Mundo} pra não fica nada pra trás…hehe

Porém, fiz igualzinho a apostila de treinamento da Caelum(FJ-21) ensina no exercício.

Criar o pacote action(escolhi esse no caso)

Pegar os .jar’s do struts2 e definir na aplicação (não todos, pq segundo a apostila, não estamos usando o Spring)

e criar o mapeamento do FilterDispatcher do Struts2 no web.xml

e o menos importante creio eu que é a página olaMundoStruts.jsp que serve como camada de apresentação da aplicaçãozinha!

A seguir segue o que implementei:

WEB.XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

CLASSE OlaMundoAction.java

package action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Result;

public class OlaMundoAction {
    @Action(
        value = "olaMundoStruts", results = {@Result(location = "/olaMundoStruts.jsp", name = "ok") }
    )
    public String execute(){
        System.out.println("Execução da Lógica com Struts2!");
        return "ok";
    }
}

ADIANTANDO, O ERRO OCORRE ENTRE AS LINHAS 7 E 9 DA CLASSE olaMundoAction.java
Segue o erro gerado pelo NETBEANS

init:
deps-module-jar:
deps-ear-jar:
deps-jar:
Created dir: C:\Documents and Settings\THIAGO\Desktop\Struts2\build\web\WEB-INF\classes
Created dir: C:\Documents and Settings\THIAGO\Desktop\Struts2\build\web\META-INF
Copying 1 file to C:\Documents and Settings\THIAGO\Desktop\Struts2\build\web\META-INF
Copying 3 files to C:\Documents and Settings\THIAGO\Desktop\Struts2\build\web
library-inclusion-in-archive:
library-inclusion-in-manifest:
Created dir: C:\Documents and Settings\THIAGO\Desktop\Struts2\build\empty
Compiling 1 source file to C:\Documents and Settings\THIAGO\Desktop\Struts2\build\web\WEB-INF\classes
C:\Documents and Settings\THIAGO\Desktop\Struts2\src\java\action\OlaMundoAction.java:7: incompatible types
found   : com.opensymphony.xwork2.Action
required: java.lang.annotation.Annotation
    @Action(
1 error
C:\Documents and Settings\THIAGO\Desktop\Struts2\nbproject\build-impl.xml:543: The following error occurred while executing this line:
C:\Documents and Settings\THIAGO\Desktop\Struts2\nbproject\build-impl.xml:310: Compile failed; see the compiler error output for details.
FALHA NA CONSTRUÇÃO (tempo total: 0 segundos)

PRA NÃO FALTA NADA, A PÁGINA olaMundoStruts.jsp

<%-- 
    Document   : olaMundoStruts
    Created on : 23/06/2011, 23:42:53
    Author     : THIAGO
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Struts2</title>
    </head>
    <body>
        <h2>Olá Mundo com Struts2!</h2>
    </body>
</html>

Está faltando algo nesse código ou configurei alguma coisa errada na aplicação (Convenções, web.xml, enfim…hehe)?

Desde já agradeço a todos…Valeuuww

4 Respostas

FacaNaCaveira

Fala ai Thiago,blz???

Cara esse erro nao sera do parenteses que vc abriu logo depois do @Action , na linha 7 e fechou logo depois na linha 9???
Tipo, por mim essa abertura e fechamento de parenteses esta meio perdida por ai , dentro da CLASSE OlaMundoAction.java

fabiofalci

com.opensymphony.xwork2.Action não é uma annotations, mas sim uma interface.
http://struts.apache.org/2.0.6/struts2-core/apidocs/com/opensymphony/xwork2/Action.html

A que vc quer é

org.apache.struts2.convention.annotation.Action

http://struts.apache.org/2.1.8/struts2-plugins/struts2-convention-plugin/apidocs/org/apache/struts2/convention/annotation/Action.html

Thiago_de_Paula_Bese

FacaNaCaveira:
Fala ai Thiago,blz???

Cara esse erro nao sera do parenteses que vc abriu logo depois do @Action , na linha 7 e fechou logo depois na linha 9???
Tipo, por mim essa abertura e fechamento de parenteses esta meio perdida por ai , dentro da CLASSE OlaMundoAction.java

Oh Blz sim FacaNaCaveira.

Cara, fiz os confere aqui. Aparentemente, ou melhor coincidentemente ta igual a apostila…hehe. Creio que não será isso cara. Cheguei ate a copia e cola o código da apostila, porém com o mesmo erro.

Mas valeu pela atenção ae brother…

Abraço…

Thiago_de_Paula_Bese

fabiofalci:
com.opensymphony.xwork2.Action não é uma annotations, mas sim uma interface.
http://struts.apache.org/2.0.6/struts2-core/apidocs/com/opensymphony/xwork2/Action.html

A que vc quer é

org.apache.struts2.convention.annotation.Action

http://struts.apache.org/2.1.8/struts2-plugins/struts2-convention-plugin/apidocs/org/apache/struts2/convention/annotation/Action.html

Realmente cara, deve ser isso!
Esses Import’s ai foram gerados automaticamente pelo NETBEANS =/

Chegando em casa vo declara esse import que vc passo, agora nem pensa por que to no trabalho hehe.

Valeu cara.

abraço

Criado 23 de junho de 2011
Ultima resposta 24 de jun. de 2011
Respostas 4
Participantes 3