PESSOAL, BOA NOITE!
Estou estudando um pouco de struts2 e para aproveitar estou desenvolvendo um portal de notícias com Struts2.
O problema é mais ou menos o seguinte:
Tenho as seguintes classes:
Classe Pais
package br.com.ujc.modelo;
import java.util.List;
public class Pais {
private Long idPais;
private String nomePais;
private String codigoDeArea;
private List<Regiao> regioes;
public Pais(){
}
public Pais(List<Regiao> regioes){
this.regioes = regioes;
}
public Long getIdPais() {
return idPais;
}
public void setIdPais(Long idPais) {
this.idPais = idPais;
}
public String getNomePais() {
return nomePais;
}
public void setNomePais(String nomePais) {
this.nomePais = nomePais;
}
public String getCodigoDeArea() {
return codigoDeArea;
}
public void setCodigoDeArea(String codigoDeArea) {
this.codigoDeArea = codigoDeArea;
}
public List<Regiao> getRegioes() {
return regioes;
}
public void setRegioes(List<Regiao> regioes) {
this.regioes = regioes;
}
}
Classe Regiao
package br.com.ujc.modelo;
public class Regiao {
private Long idRegiao;
private String nomeRegiao;
private Pais pais;
public Long getIdRegiao() {
return idRegiao;
}
public void setIdRegiao(Long idRegiao) {
this.idRegiao = idRegiao;
}
public String getNomeRegiao() {
return nomeRegiao;
}
public void setNomeRegiao(String nomeRegiao) {
this.nomeRegiao = nomeRegiao;
}
public Pais getPais() {
return pais;
}
public void setPais(Pais pais) {
this.pais = pais;
}
}
Classe PaisDAO
public class PaisDAO implements PaisInterface {
private final Connection connection;
public PaisDAO(){
try{
this.connection = new ConnectionFactory().getConnection();
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
private void adiciona(Pais pais){
String sql = "insert into Pais (nmPais, cdArea) values (?,?)";
PreparedStatement stmt;
try{
stmt = connection.prepareStatement(sql);
stmt.setString(1, pais.getNomePais());
stmt.setString(2, pais.getCodigoDeArea());
stmt.execute();
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
public List<Pais> buscaPorNomeSimilar(Pais pais){
String sql = "select nmPais from Pais where nmPais like = ? ";
PreparedStatement stmt;
try{
List<Pais> paises = new ArrayList<Pais>();
stmt = connection.prepareStatement(sql);
stmt.setString(1,"%"+ pais.getNomePais() + "%");
ResultSet rs = stmt.executeQuery();
while(rs.next()){
paises.add(populaPais(rs));
}
rs.close();
stmt.close();
return paises;
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
//Retorna um pais por nome
public Pais buscaPorNome(Pais pais){
String sql = "select nmPais from Pais where nmPais = ?";
PreparedStatement stmt;
try{
stmt = connection.prepareStatement(sql);
stmt.setString(1,"'"+ pais.getNomePais() + "'");
ResultSet rs = stmt.executeQuery();
populaPais(rs);
rs.close();
stmt.close();
return pais;
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
public List<Pais> listaPais(){
String sql = "select * from Pais order by nmPais";
PreparedStatement stmt;
try{
List<Pais> paises = new ArrayList<Pais>();
stmt = connection.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while(rs.next()){
paises.add(populaPais(rs));
}
rs.close();
stmt.close();
return paises;
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
public Pais populaPais(ResultSet rs) throws SQLException{
Pais pais = new Pais();
pais.setIdPais(rs.getLong("idPais"));
pais.setNomePais(rs.getString("nmPais"));
pais.setCodigoDeArea(rs.getString("cdArea"));
return pais;
}
@Override
public boolean ExistePais(Pais pais) {
String sql = "select * from Pais where nmPais = ?";
PreparedStatement stmt;
try{
stmt = connection.prepareStatement(sql);
stmt.setString(1, pais.getNomePais());
ResultSet rs = stmt.executeQuery();
if(rs.next()){
stmt.close();
rs.close();
return true;
}else{
adiciona(pais);
stmt.close();
rs.close();
return false;
}
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Classe RegiaoDAO
package br.com.ujc.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import br.com.ujc.ConnectionFactory;
import br.com.ujc.interfaces.RegiaoInterface;
import br.com.ujc.modelo.Pais;
import br.com.ujc.modelo.Regiao;
public class RegiaoDAO implements RegiaoInterface {
public final Connection connection;
public RegiaoDAO(){
try{
this.connection = new ConnectionFactory().getConnection();
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
private void adiciona(Regiao regiao){
String sql = "insert into Regiao (nmRegiao) values(?)";
PreparedStatement stmt;
try{
stmt = connection.prepareStatement(sql);
stmt.setString(1,regiao.getNomeRegiao());
stmt.execute();
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
public List<Regiao> buscaPorNomeSimilar(Regiao regiao){
String sql = "select nmRegiao from Regiao where nmRegiao like = ?";
PreparedStatement stmt;
try{
List<Regiao> regioes = new ArrayList<Regiao>();
stmt = connection.prepareStatement(sql);
stmt.setString(1, "%" + regiao.getNomeRegiao() + "%");
ResultSet rs = stmt.executeQuery();
while(rs.next()){
regioes.add(populaRegiao(rs));
}
stmt.close();
rs.close();
return regioes;
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
public Regiao buscaPorNome(Regiao regiao){
String sql = "select nmRegiao from Regiao where nmRegia = ?";
PreparedStatement stmt;
try{
stmt = connection.prepareStatement(sql);
stmt.setString(1, "'" + regiao.getNomeRegiao() + "'");
ResultSet rs = stmt.executeQuery();
populaRegiao(rs);
stmt.close();
rs.close();
return regiao;
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
public List<Regiao> listaRegiao(){
String sql = "select * from Regiao order by nmRegiao";
PreparedStatement stmt;
try{
List<Regiao> regioes = new ArrayList<Regiao>();
stmt = connection.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while(rs.next()){
regioes.add(populaRegiao(rs));
}
rs.close();
stmt.close();
return regioes;
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
public Regiao populaRegiao(ResultSet rs) throws SQLException{
Regiao regiao = new Regiao();
regiao.setIdRegiao(rs.getLong("idRegiao"));
regiao.setNomeRegiao(rs.getString("nmRegiao"));
return regiao;
}
public void adicionaRegioesAoPais(List<Regiao> regioes, Pais pais){
String sql = "insert into PaisRegiao (Pais_idPais, Regiao_diRegiao) values(?,?)";
PreparedStatement stmt;
System.out.println(pais.getNomePais());
try{
stmt = connection.prepareStatement(sql);
for(int i = 0; i <= regioes.size(); i++) {
if(regioes.get(i).getIdRegiao() != null){
stmt.setLong(1, pais.getIdPais());
stmt.setLong(2, regioes.get(i).getIdRegiao());
stmt.execute();
System.out.println(regioes.get(i).getIdRegiao());
}
}
stmt.close();
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean existeRegiao(Regiao regiao) {
String sql = "select nmRegiao from Regiao where nmRegiao = ?";
PreparedStatement stmt;
try{
stmt = connection.prepareStatement(sql);
stmt.setString(1, regiao.getNomeRegiao());
ResultSet rs = stmt.executeQuery();
if(rs.next()){
stmt.close();
rs.close();
return true;
}else{
adiciona(regiao);
stmt.close();
rs.close();
return false;
}
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
E pro fim a action RegiaoPaisAdicionarAction
public class RegiaoPaisAdicionarAction {
private List<Regiao> regioes;
private Pais pais;
@Action(value = "regiaoPaisAdicionar", results = {
@Result(location = "regiaopaisadicionado.html", name = "ok")
})
public String execute(){
new RegiaoDAO().adicionaRegioesAoPais(regioes, pais);
return "ok";
}
public void setPais(Pais pais){
this.pais = pais;
System.out.println(pais.getIdPais());
}
public Pais getPais(){
return this.pais;
}
public void setRegioes(List<Regiao> regioes){
this.regioes = regioes;
}
public List<Regiao> getRegiao(){
return this.regioes;
}
}
Tenho uma página que para o cadastro
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>UJC NOTICIAS - CADASTRO DE REGIÕES DE UM PAIS</title>
</head>
<body>
<form action="regiaoPaisAdicionar" method="get">
Cadastro de regiões de um pais <br />
Selecione um pais:
<select name="pais">
<option value="">Selecione</option>
<c:forEach var="pais" items="${paises}">
<option value="${pais.idPais}">${pais.nomePais}</option>
</c:forEach>
</select>
<br />
Selecione as regiões que esse pais contém:
<table cellpadding="0" cellspacing="0" border="1">
<tr>
<td>*</td>
<td>Lista de Regiões</td>
</tr>
<c:forEach var="regiao" items="${regioes}">
<tr>
<td>
<input type="checkbox" name="regiao" value="${regiao.idRegiao}" />
</td>
<td>${regiao.nomeRegiao}</td>
</tr>
</c:forEach>
</table>
<input type="submit" value="Gravar">
</form>
</body>
</html>
Ela é carregada com uma action que contem uma lista de pais e uma lista de regioes
Um erro ocorre no metodo setPais da action…
Porque isso ocorre?
Alguém pode me ajudar?
Erro
WARNING: Error setting expression 'pais' with value '[Ljava.lang.String;@6364cbde'
ognl.MethodFailedException: Method "setPais" failed for object br.com.ujc.action.RegiaoPaisAdicionarAction@79b4748 [java.lang.NoSuchMethodException: br.com.ujc.action.RegiaoPaisAdicionarAction.setPais([Ljava.lang.String;)]
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1265)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1454)
at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85)
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162)
at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:28)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2225)
at com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor.setProperty(CompoundRootAccessor.java:65)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2225)
at ognl.ASTProperty.setValueBody(ASTProperty.java:127)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
at ognl.SimpleNode.setValue(SimpleNode.java:301)
at ognl.Ognl.setValue(Ognl.java:737)
at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:198)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:161)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:149)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:276)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:187)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:636)
Caused by: java.lang.NoSuchMethodException: br.com.ujc.action.RegiaoPaisAdicionarAction.setPais([Ljava.lang.String;)
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1206)
... 64 more
/-- Encapsulated exception ------------\
OBRIGADO!