Boa dia galera!! blz???
Bom… eu tô utilizando o struts 2.0.6 e estou com um probleminha ao submeter um formulário e ao realizar a validação. O problema é a codificação dos caracteres.
ex.: Ao cadastrar o seguinte valor em um textField, “Prédio Histórico”, O struts retorna para o mesmo textfield o valor “Prédio Histórico”.
Eu estou utilizando tmb o Tiles…
Segue o código da página:
templatePrincipal.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><tiles:getAsString name="title"/></title>
<tiles:insertAttribute name="linkCssJavascript"/>
</head>
<body>
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="1">
<div id="titulo"></div>
</td>
</tr>
<tr>
<td height="10" align="right">
<div id="logotipo"></div>
</td>
</tr>
<tr>
<td height="10">
<table border="0" cellspacing="0" cellpadding="0" width="100%" class="tableMenu">
<tr>
<td width="1%" align="left"><img src="recursos/imagens/barra_canto_esquerdo.png" border="0"/></td>
<td width="98%">
<a href='<s:url value="/index.action"/>'> :: Pagina Inicial</a>
<a href='<s:url value="/imovel!input.action"/>'> :: Cadastro de Imóveis</a>
<a href="#">:: </a>
</td>
<td width="1%" align="right"><img src="recursos/imagens/barra_canto_direito.png" border="0"/></td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<table><tr><td align="left">
<tiles:insertAttribute name="corpo"/>
</td></tr></table>
</td>
</tr>
</table>
</body>
</html>
linkcssJavascript.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:url id="cssLayout" value="/recursos/estilos/layout.css" />
<s:url id="cssFormulario" value="/recursos/estilos/formulario.css" />
<s:url id="scriptFocusTextField" value="/recursos/script/focusTextField.js" />
<link rel="stylesheet" type="text/css" href="${cssLayout}">
<link rel="stylesheet" type="text/css" href="${cssFormulario}">
<script charset="utf-8" type="javascript" src="${scriptFocusTextField}"></script>
<s:head theme="ajax" debug="true"/>
cadastroImovel.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:if test="imovel != null && imovel.codigo != null">
<div class="confirmacao" align="center">Imóvel cadastrado com sucesso!</div>
</s:if>
<s:form method="post" validate="true" action="imovel!salvar.action">
<s:textfield label="Nome" name="imovel.nome" required="true"/>
<s:textfield label="Endereço" name="imovel.endereco" required="true"/>
<s:textfield label="Bairro" name="imovel.bairro" required="true"/>
<s:textfield label="Cidade" name="imovel.cidade" required="true"/>
<s:select label="UF" name="imovel.uf.codigo" list="ufs" listKey="codigo" listValue="nome" labelposition="left"/>
<s:textfield label="Valor Venal" name="imovel.valorVenal" required="false"/>
<s:select label="Proprietário" name="imovel.proprietario.codigo" list="proprietarios" listKey="codigo" listValue="nome"/>
<s:submit value="Cadastrar" cssClass="botaoOK"/>
</s:form>
tiles.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!--
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
-->
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://struts.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="locroom.layout" template="/recursos/templates/templatePrincipal.jsp">
<put name="title" value="Controle de Locação de Imóveis"/>
<put name="linkCssJavascript" value="/recursos/templates/linkCssJavascript.jsp"/>
</definition>
<definition name="locroom.index" extends="locroom.layout">
<put name="corpo" value="/index.jsp"/>
</definition>
<definition name="locroom.cadastroImovel" extends="locroom.layout">
<put name="corpo" value="/cadastroImovel.jsp"/>
</definition>
</tiles-definitions>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.apache.tiles.listener.TilesListener</listener-class>
</listener>
<session-config>
<session-timeout>20</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="locroom-default" extends="tiles-default">
<action name="index" class="br.com.cet.locroom.acoes.ActionBase">
<result type="tiles">locroom.index</result>
</action>
<action name="imovel!*" class="br.com.cet.locroom.acoes.ActionImovel" method="{1}">
<result name="input" type="tiles">locroom.cadastroImovel</result>
<result name="success" type="tiles">locroom.cadastroImovel</result>
</action>
</package>
</struts>
ActionImovel
/*
* ActionImovel.java
*
* Created on 15 de Maio de 2007, 13:30
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package br.com.cet.locroom.acoes;
import br.com.cet.locroom.acoes.locator.ServiceLocatorUtil;
import br.com.cet.locroom.cadastro.CadastroLocal;
import br.com.cet.locroom.entidades.Imovel;
import br.com.cet.locroom.entidades.Proprietario;
import br.com.cet.locroom.entidades.Uf;
import br.com.cet.locroom.exceptions.ExcecaoImovelDuplicado;
import br.com.cet.locroom.pesquisas.PesquisaLocal;
import com.opensymphony.xwork2.Preparable;
import java.util.Collection;
import javax.naming.NamingException;
/**
*
* @author Administrador
*/
public class ActionImovel extends ActionBase implements Preparable{
// private static final Log logger = LogFactory.getLog(ActionImovel.class);
private Imovel imovel;
private Collection<Proprietario> proprietarios;
private Collection<Uf> ufs;
private CadastroLocal cadastro;
private PesquisaLocal pesquisa;
public void prepare() throws NamingException{
cadastro = ServiceLocatorUtil.getCadastroService();
pesquisa = ServiceLocatorUtil.getPesquisaService();
proprietarios = pesquisa.getProprietarios();
ufs = pesquisa.getUfs();
System.out.println("INFO: >> Prepare >>>> Passou aqui." + ActionImovel.class);
}
/**
* @return Retorna a constante ActionSupport.SUCCESS caso o cadastro do
* imóvel finalize corretamente, caso contrario uma exceção será lançada.
**/
public String salvar() throws ExcecaoImovelDuplicado{
System.out.println("INFO: >> salvar >>>> Nome: " + imovel.getNome());
cadastro.addImovel(imovel);
return SUCCESS;
}
public Imovel getImovel() {
return imovel;
}
public void setImovel(Imovel imovel) {
this.imovel = imovel;
}
public Collection<Proprietario> getProprietarios() {
return proprietarios;
}
public void setProprietarios(Collection<Proprietario> proprietarios) {
this.proprietarios = proprietarios;
}
public Collection<Uf> getUfs() {
return ufs;
}
public void setUfs(Collection<Uf> ufs) {
this.ufs = ufs;
}
}
ActionImovel-validation.xml
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="imovel.nome">
<field-validator type="requiredstring">
<message>O campo nome é obrigatório</message>
</field-validator>
</field>
<field name="imovel.endereco">
<field-validator type="requiredstring">
<message>O campo endereço é obrigatório</message>
</field-validator>
</field>
<field name="imovel.bairro">
<field-validator type="requiredstring">
<message>O campo bairro é obrigatório</message>
</field-validator>
</field>
<field name="imovel.cidade">
<field-validator type="requiredstring">
<message>O campo cidade é obrigatório</message>
</field-validator>
</field>
<!--
<field name="imovel.valorVenal">
<field-validator type="requiredstring">
<message key="requiredstring"/>
</field-validator>
</field>
<field name="imovel.valorVenal">
<field-validator type="double">
<message>Formato de número inválido.</message>
</field-validator>
</field>
<field name="imovel.proprietario.codigo">
<field-validator type="requiredstring">
<message key="requiredstring"/>
</field-validator>
</field>
<field name="imovel.uf.codigo">
<field-validator type="requiredstring">
<message key="requiredstring"/>
</field-validator>
</field>
-->
</validators>