在struts2中验证失败时找不到列出的数据

时间:2017-10-31 20:05:04

标签: java validation jsp combobox struts2

我是Struts的新手,我必须使用带注释的struts2向现有系统添加一个模块。我必须使用带有JDK6的struts 2.3.34版本。

我搞砸了struts2验证。问题是我的表单包含<s:select />,当验证失败时,服务器会抱怨找不到<s:select />的集合或列表。如果我删除该标记,验证工作正常。

我的代码段如下: (JSP)

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<body>
<h2>Enter Violation Number<h2>

<s:form method="post" action="searchViolation" namespace="/Broker">
    <s:textfield name="violationNumber" label="Name" maxlength="10"/>
    <s:submit value="Search" name="submit" />
</s:form>
</body>
</html>

支持Java类:

@Namespace("/Broker")
@ResultPath(value = "/")
@Results({
    @Result(name = "success", location = "pages/enter_defense.jsp"),
    @Result(name = "input", location = "pages/search_violation.jsp"),
})
@InterceptorRefs ({
    @InterceptorRef("defaultStack")
})
public class ViolationNumberAction extends ActionSupport implements Preparable {

    private static final long serialVersionUID = 2137861793381499464L;

    private String violationNumber;
    private static List<String> states;

    @Override
    public void prepare() throws Exception {
    states = new ArrayList<String>();
    states.add("New York");
    }

    @Action(value = "searchViolation")
    public String execute() {

    return SUCCESS;
    }

    /**
     * @return the violationNumber
     */
    @RequiredStringValidator(fieldName="violationNumber", message="Violation Number is rquired.", trim=true)
    public String getViolationNumber() {
    return violationNumber;
    }

如果违规号不为空,则转发视图的下一个JSP如下:

<s:form method="post" action="create_hearing" namespace="/Broker">
    <table>
        <tr>
            <td><s:textfield name="firstName" label="First Name"
                    labelposition="top" maxlength="30" errorPosition="top" /></td>
        </tr>
        <tr>
            <td><s:textfield name="address" label="Address"
                    labelposition="top" maxlength="50" /></td>
        </tr>
        <tr>
            <td>
                <s:select
                    label="State/Province" value="state" name="states"
                    labelposition="top" headerValue="--- Please Select ---"
                    headerKey="1" list="states" />
            </td>
        </tr>
        <tr>
            <td><s:submit value="Continue" name="submit"
                    onclick="validate()" /></td>
            <td><s:submit value="Cancel" name="submit" /></td>
        </tr>
    </table>
</s:form>

因此,支持java类如下:

@Namespace("/Broker")
@ResultPath(value = "/")
@Results({
    @Result(name = "success", location = "pages/verify_info.jsp"),
    @Result(name = "input", location = "Broker/pages/enter_defense.jsp"),
})
@InterceptorRefs ({
    @InterceptorRef("defaultStack"), 
    @InterceptorRef("prepare")
})
public class CreateNewHearingAction extends ActionSupport {

    private static final long serialVersionUID = -5864237156298942117L;

    private String firstName;
    private String address;
    private String state;

    @Action(value = "/create_hearing")
    @InputConfig(methodName="input")
    public String execute() {

    return SUCCESS;
    }

    public String state() throws Exception {
    return SUCCESS;
    }

问题是当验证失败时,会显示以下错误:

Struts has detected an unhandled exception:

Messages:   
tag 'select', field 'list', name 'states': The requested list key 'states' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name}
tag 'select', field 'list', name 'states': The requested list key 'states' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
File:   org/apache/jasper/servlet/JspServletWrapper.java
Line number:    480

有任何建议/解决方案吗?

0 个答案:

没有答案