Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java EE (J2EE) и Spring > Проблема с валидацией в Struts2 + Tiles


Автор: Ares4322 10.2.2010, 10:52
Доброго времени суток!
Использую Struts2+Tiles. Нужно, чтобы при ошибке ввода показывалась страничка, сформированная по шаблону registration. Как написать struts.xml? Вот код.
Код

<struts>
    <package extends="struts-default">
        <result-types>
            <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
        </result-types>
        <action name="main" class=".Main">
            <result>main.jsp</result>
        </action>
        <action name="*Link" method="{1}" class=".web.LinkAction">
            <result name="main" type="tiles">main</result>
            <result name="news" type="tiles">news</result>
            <result name="cabinet" type="tiles">cabinet</result>
            <result name="partners" type="tiles">partners</result>
            <result name="contacts" type="tiles">contacts</result>
            <result name="registration" type="tiles">registration</result>
        </action>
        <action name="registration" class="logic.Registration">
            <result name="success">registrationSuccess.jsp</result>
            <result  name="input" type="chain">
                <param name="actionName">registrationLink</param>
                <param name="method">registration</param>
            </result>
        </action>
    </package>
</struts>


Код

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
packagelogic;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class Registration extends ActionSupport {

    public boolean register() {
        boolean b = true;
        return b;
    }

    public boolean login() {
        boolean b = true;
        return b;
    }

    public String execute() throws Exception {
        return Action.SUCCESS;
    }
    private String name;
    private String surname;
    private String login;
    private String password;
    private String confirm;
    private String email;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the surname
     */
    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    private boolean isEmptyString(String value) {
        return value == null || "".equals(value.trim());
    }

    public void validate() {
        if (isEmptyString(name)) {
            addFieldError("name", "Не указано имя");
        }
        if (isEmptyString(surname)) {
            addFieldError("surname", "Не указано имя");
        }
        if (isEmptyString(getLogin())) {
            addFieldError("login", "Не указан логин");
        }
        if (isEmptyString(getPassword())) {
            addFieldError("password", "Не указан пароль");
        }
        if (isEmptyString(getConfirm())) {
            addFieldError("confirm", "Не указано подтверждение пароля");
        }
        if (!isEmptyString(password) && !isEmptyString(confirm)) {
            if (!password.equals(confirm)) {
                addActionError("Не совпадают пароль и его подтверждение");
            }
        }
    }

    /**
     * @return the login
     */
    public String getLogin() {
        return login;
    }

    /**
     * @param login the login to set
     */
    public void setLogin(String login) {
        this.login = login;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the confirm
     */
    public String getConfirm() {
        return confirm;
    }

    /**
     * @param confirm the confirm to set
     */
    public void setConfirm(String confirm) {
        this.confirm = confirm;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }
}



Добавлено через 6 минут и 33 секунды
сделал.
после
<result name="registration" type="tiles">registration</result>
написал
<result name="input" type="tiles">registration</result>
Но получается как то коряво и неправильно.
Посоветуйте, как улучшить?

Автор: MaxPayneC 10.2.2010, 14:22
Заставить метод execute() возвращать INPUT в случае проблем с входными данными и ERROR в случае проблем иного характера, а потом дописать в конфиге экшна 
Код

<result name="input">your_error_jsp.jsp</result>

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)