You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Célio Cidral Junior <cc...@gmail.com> on 2007/06/20 19:07:08 UTC

[S2] Incorrect validation behaviour

I have a couple of actions with validation configuration. The
validation works fine for most of them, except for one in which case
all the fields are always invalidated even when I fill them correctly.
Also, I noticed that, after submitting the form, the values of the
fields are not preserved. I can't understand why it doesn't work
right, because I simply copied and pasted from existing files of
another action. I've already spent hours trying to fix that, but can't
figure out what's wrong.


alteracao-senha-form.jsp
-----------------------------------
<%@ taglib prefix="s" uri="/struts-tags"%>

<html>
	<head>
		<title><s:property value="%{getText('alteracao-senha')}"/></title>
	</head>
	<body>
		<s:actionerror/>
		<s:actionmessage/>
		<s:fielderror/>
		<br>
		<s:form action="alteracaoDeSenha!salvar">
			<s:token/>
			
			<s:textfield name="senhaAtual"
label="%{getText('alteracao-senha.senha-atual')}"/>
			<s:textfield name="novaSenha"
label="%{getText('alteracao-senha.nova-senha')}"/>
			<s:textfield name="confirmacaoDaSenha"
label="%{getText('alteracao-senha.confirmacao-senha')}"/>
			
			<s:submit value="%{getText('registro.salvar')}"/>
		</s:form>
		
		<a href="javascript:void(null);" onclick="history.go(-1);"/><s:text
name="navegacao.voltar"/></a>
	</body>
</html>


AlteracaoDeSenhaAction.java
------------------------------------------
package br.com.radice.labore.web.actions;

import br.com.radice.labore.Usuario;
import br.com.radice.labore.dao.UsuarioDao;
import br.com.radice.labore.web.SessionKeys;
import br.com.radice.web.struts2.actions.SessionProvidedAction;

public class AlteracaoDeSenhaAction extends SessionProvidedAction {
	private static final long serialVersionUID = 1L;
	
	private UsuarioDao usuarioDao;
	private String senhaAtual, novaSenha, confirmacaoDaSenha;
	
	public AlteracaoDeSenhaAction() {
	}
	
	public void setConfirmacaoDaSenha(String confirmacaoDaSenha) {
		this.confirmacaoDaSenha = confirmacaoDaSenha;
	}

	public void setNovaSenha(String novaSenha) {
		this.novaSenha = novaSenha;
	}

	public void setUsuarioDao(UsuarioDao usuarioDao) {
		this.usuarioDao = usuarioDao;
	}
	
	public void setSenhaAtual(String senha) {
		this.senhaAtual = senha;
	}
	
    public String salvar() throws Exception {
    	Usuario usuarioLogado = getUsuarioLogado();
    	
    	if (!usuarioLogado.possuiEstaSenha(senhaAtual)) {
    		addActionError(getText("alteracao-senha.senha-atual-incorreta"));
    		return INPUT;
    	}
    	
    	if (!novaSenha.equals(confirmacaoDaSenha)) {
    		addActionError(getText("alteracao-senha.nova-senha-nao-confere"));
    		return INPUT;
    	}
    	
    	usuarioLogado.setSenha(novaSenha);
    	
    	usuarioDao.save(usuarioLogado);
    	
		addActionMessage(getText("alteracao-senha.alterada-com-sucesso"));
		
    	return SUCCESS;
    }

	private Usuario getUsuarioLogado() {
		Usuario usuarioLogado = (Usuario) getSession().get(SessionKeys.USUARIO);
		
		return usuarioDao.get(usuarioLogado.getId());
	}
}


AlteracaoDeSenhaAction-validation.xml
--------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<!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="senhaAtual">
        <field-validator type="requiredstring">
            <message key="alteracao-senha.campo-obrigatorio.senha-atual"/>
        </field-validator>
    </field>
    <field name="novaSenha">
        <field-validator type="requiredstring">
            <message key="alteracao-senha.campo-obrigatorio.nova-senha"/>
        </field-validator>
    </field>
    <field name="confirmacaoDaSenha">
        <field-validator type="requiredstring">
            <message key="alteracao-senha.campo-obrigatorio.confirmacao-senha"/>
        </field-validator>
    </field>
</validators>


spring.actions.xml
--------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<!-- other stuff omitted -->

	<bean id="alteracaoDeSenhaAction"
class="br.com.radice.labore.web.actions.AlteracaoDeSenhaAction"
scope="prototype">
		<property name="usuarioDao" ref="usuarioDao"/>
	</bean>
</beans>


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>
	<!-- other stuff omitted -->

	<package name="private" namespace="/private" extends="secure">
		<!-- other stuff omitted -->
		<action name="iniciar">
			<result>/jsp/home.jsp</result>
		</action>
		<action name="alteracaoDeSenha" class="alteracaoDeSenhaAction">
			<result name="input">/jsp/alteracao-senha-form.jsp</result>
			<result name="success" type="redirect-action">iniciar</result>
		</action>
	</package>
</struts>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org