Struts 2中的验证和主题 [英] The validation and theme in Struts 2

查看:121
本文介绍了Struts 2中的验证和主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个Struts 2项目的工作,问题是,我使用
<常量名=struts.ui.themeVALUE =简单/> 在我的 struts.xml 为我的JSP页面的布局(例如安排2-3文本文件在一行使用tablecode)但由于 theme =simple

$

$ p

,我无法在同一个jsp页面上显示验证错误。 配置:

 < struts& 
<! - 默认包的配置。 - >
< constant name =struts.ui.themevalue =simple/>
< package name =defaultextends =struts-default>
< action name =validateUserclass =login.LoginAction>
< result name =SUCCESS> /welcome.jsp< / result>
< result name =input> /login.jsp< / result>
< / action>
< / package>
< / struts>

操作:

  public class LoginAction extends ActionSupport {

private String username; // getter和setter
private String password; // getter和setter


@Override
public String execute(){
//这里有一些业务逻辑....
returnSUCCESS ;
}
//简单的验证
@覆盖
公共无效的validate(){
如果(。等于(getUsername())){
addFieldError(username,getText(username.required));
}
if(。equals(getPassword())){
addFieldError(password,getText(password.required));
}
}
}

/ STRONG>



<预类=郎HTML prettyprint-覆盖> < S:形式行动=的ValidateUser验证=真正的> ;
< table>
< tr>
< td>用户名< / td>
< td>< s:textfield label =usernamename =username/>< td />
< / tr>
< tr>
< td> password< / td>
< td>< s:password label =passwordname =password/>< td />
< tr>
< td> < s:submit label =submitname =submit/>< / td>
< / tr>
< / table>
< / s:form>有没有办法使用我的CSS维护布局,并使用Struts 2验证?

解决方案

当然! XHTML主题会自动为您的输入标签添加 fieldError 标签;



当您使用简单主题时,您需要手动添加它们,并为您的标签添加一个ID以匹配它们(除非它是自动生成的, ):

 < td> 
< s:textfield id =usernamelabel =usernamename =username/>
< s:fielderror fieldName =username/>
< / td>

< td>
< s:password id =passwordlabel =passwordname =password/>
< s:fielderror fieldName =password/>
< / td>






PS:我想这些都是拼写错误错误只是问题,而不是在真正的代码,但你有:




  • 自我关闭 TD />

  • 一个未关闭< TR>

  • a < tr> 与一个< td> > colspan =2指定。


I am working on a Struts 2 project ,the problem is that I am using <constant name="struts.ui.theme" value="simple"/> in my struts.xml for the layout of my JSP page(e.g arranging 2-3 textfiled in one line using tablecode ) as per the CSS applied, but I am not able show the validation error on the same jsp page due to theme="simple".

Configuration:

<struts>
    <!-- Configuration for the default package. -->
    <constant name="struts.ui.theme" value="simple"/>
    <package name="default"   extends="struts-default">
        <action name="validateUser" class="login.LoginAction">
            <result name="SUCCESS">/welcome.jsp</result>
            <result name="input">/login.jsp</result>
        </action>
    </package>
</struts>

Action:

public class LoginAction extends ActionSupport{

    private String username; // getter and setter
    private String password; // getter and setter


    @Override
    public String execute() {
        // some business logic here....
        return "SUCCESS";    
    }
    //simple validation
    @Override 
    public void validate(){             
        if("".equals(getUsername())){
            addFieldError("username", getText("username.required"));
        }
        if("".equals(getPassword())){
            addFieldError("password", getText("password.required"));
        }
    }       
}

View:

<s:form action="validateUser" validate="true" >
    <table>
        <tr>
            <td>username</td>
            <td><s:textfield  label="username" name="username" /><td/>
        </tr>
        <tr>
            <td>password</td>
            <td><s:password  label="password" name="password" /><td/>
        <tr> 
            <td> <s:submit  label="submit" name="submit"/></td>
        </tr>
     </table>
</s:form>

Is there a way to maintain the layout with my CSS and also use Struts 2 validation ?

解决方案

Sure! The XHTML theme will automatically add a fieldError tag to your input tags;

when using the Simple theme, instead, you need to add them manually, and give an id to your tags to match them (unless it would be autogenerated, and more difficult to spot):

<td>
    <s:textfield id="username" label="username" name="username" />
    <s:fielderror fieldName="username" />
</td>

<td>
    <s:password id="password" label="password" name="password" />
    <s:fielderror fieldName="password" />
</td>


P.S: I guess these are in the typos and errors are in the question only, and not in the real code, but you have:

  • self closing <td/>,
  • an unclosed <tr>, and
  • a <tr> with a single <td> without a colspan="2" specified.

这篇关于Struts 2中的验证和主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆