简体   繁体   English

Struts 2中的Java类内部验证

[英]Validation inside Java class in Struts 2

I am going to pass error message from inside the Java class to JSP and this error message is written in .properties file. 我将错误消息从Java类内部传递到JSP,并且此错误消息写在.properties文件中。

I am using action class method addActionError(result) to display that error message but it is displaying the message as error.register.bademail in JSP. 我正在使用操作类方法addActionError(result)显示该错误消息,但在JSP error.register.bademail其显示为error.register.bademail This is not my written message. 这不是我的书面信息。

Java class: Java类:

package com.uttarainfo.s2;
public class Model {

    public List<String> register(RegBean bean) {
        if(bean.getEmail().equals("bond@gmail.com"))
            return "error.register.bademail"; i want to return this key 
        else
            return "success";
    }

}

Action class: 动作类:

if(result.equals(SUCCESS))
    return SUCCESS;
else
{
    addActionError(result);
    return "failure";
}

This is JSP code: 这是JSP代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s" %>    
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1>Register</h1>
    <s:form action="register"  method="post" enctype="multipart/form-data">
        <s:textfield key="bean.uname"/>
        <s:textfield key="bean.email"/>
        <s:password key="bean.pwd"/>
        <s:password key="bean.rpwd"/>
        <s:file key="bean.pic"/>
        <s:submit/>
        <s:actionerror/>
    </s:form>
</body>
</html>

Try 尝试

addActionError(getText(result));

getText() is used to retrieve localized messages form resources. getText()用于检索资源形式的本地化消息。 All you need to supply the key. 您只需要提供钥匙。

I want to see struts.xml as well as the properties file. 我想查看struts.xml以及属性文件。 please put the code. 请输入代码。

EDIT: Ok, so what i understood from your edit is that you have to return a list (as you do in java) instead of string from the action class. 编辑:好的,所以我从您的编辑中了解到,您必须返回一个列表(就像在Java中一样),而不是从操作类返回字符串。 If yes you can not do so. 如果是,则不能这样做。 However,use struts-2-json plugin to return json objects to the view. 但是,请使用struts-2-json插件将json对象返回到视图。

http://www.mkyong.com/struts2/struts-2-and-json-example/ http://www.mkyong.com/struts2/struts-2-and-json-example/

Also , it does not matter what type of data is returned by your action method as you can only return string but what attributes you set in your action class. 而且,操作方法返回什么类型的数据都没有关系,因为您只能返回字符串,但可以返回在操作类中设置的属性。 So, the attributes set in action class will be accessible in your view & for List(set in action class) you will have to use iterator. 因此,可以在视图中访问在操作类中设置的属性,对于必须使用迭代器的List(在操作类中设置)也可以访问。

Moreover if you have just stated with struts2 I will recommend you understand the MVC & role of getters & setters in it. 此外,如果您刚刚对struts2进行了说明,我建议您了解其中的getter和setter的MVC和角色。

Can I use mvc without getters and setters? 我可以在没有getter和setter的情况下使用mvc吗?

Thanks for the input, if you get error then also you should return a string. 感谢您的输入,如果出现错误,还应该返回一个字符串。 with that string the mapped error page in struts.xml will be shown. 使用该字符串,将显示struts.xml中的映射错误页面。 For eg. 例如。 while processing data you found it inconsistent then you can return a string "error" and in struts it appears as 处理数据时发现不一致,则可以返回字符串“错误”,并且在struts中显示为

<action name="yourAction" class="action.Action">
<result name="error">/Error.jsp</result>
</action>

And for exception handling which is what you should be looking for or will soon you can go to the link below http://struts.apache.org/release/2.3.x/docs/exception-handling.html 对于异常处理,这是您应该寻找的或不久将要找到的,您可以转到下面的链接http://struts.apache.org/release/2.3.x/docs/exception-handling.html

Please tell me if I answered your question. 请告诉我我是否回答您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM