Struts使用xml验证用户输入

时间:2018-10-29 13:25:36

标签: xml validation action struts interceptor

我正在尝试通过xml验证我的用户输入,如本智能教程中所述:https://struts.apache.org/getting-started/form-validation-using-xml.html

不幸的是,到目前为止,这还行不通。如果有人可以帮助我,我会很高兴。我正在尝试向我的CreateSubjectAction.class添加一个验证,该验证至少看起来是这样的:

public class CreateSubjectAction extends ActionSupport {

private Map<Long, CourseOfStudies> courseOfStudiesMap;
private CourseOfStudiesService courseOfStudiesService;
private SubjectService subjectService;
private String subjectDescription;
private String[] courseOfStudiesSelected;

public String display() {
    initialise();
    return NONE;
}

private void initialise() {
    courseOfStudiesMap = new HashMap<>();
    List<EntityObject> courseOfStudiesList = courseOfStudiesService.listEntities(new CourseOfStudies());
    for (EntityObject courseOfStudies : courseOfStudiesList) {
        CourseOfStudies course = (CourseOfStudies) courseOfStudies;
        courseOfStudiesMap.put(course.getId(), course);
    }
}

@Override
public String execute() throws Exception {
    initialise();
    Subject subjectNew = new Subject();
    subjectNew.setSubjectDescription(subjectDescription);
    List<CourseOfStudies> courseOfStudies = new ArrayList<>();
    for (String co : courseOfStudiesSelected) {
        CourseOfStudies studyCourse = courseOfStudiesMap.get(Long.parseLong(co));
        courseOfStudies.add(studyCourse);    

然后我得到了CreateSubjectAction-validate.xml:

<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
    "http://struts.apache.org/dtds/xwork-validator-1.0.dtd">

<validators>
    <validator type="requiredstring">
        <param name="fieldName">subjectDescription</param>
        <message>Blaah</message>
    </validator>
</validators>

最后但并非最不重要的是CreateSubjectForm的JSP:

<%@ page contentType="text/html;charset=UTF-8" %>
<div id="formBox">
    <h2><s:text name="menu.addsubject"/></h2>
    <hr/>
    <s:form action="saveNewCourse" validate="true">
        <s:textfield key="subject.description" name="subjectDescription" requiredLabel="true" cssClass="formInputField"
                     value="%{subject.subjectDescription}"/>
        <s:checkboxlist list="courseOfStudiesMap" key="subject.courseOfStudies" requiredLabel="true" name="courseOfStudiesSelected" value="defaultCourseOfStudies"/>
        <div id="formRow">
            <s:submit key="button.save" theme="simple" cssClass="formButton"/>
            <s:submit key="button.cancel" action="subjectList" onclick="confirm('sicher?')" theme="simple"
                      cssClass="formButton"/>
        </div>
    </s:form>
</div>

此操作的struts.xml编写如下:

        <action name="createSubject"
            class="de.nordakademie.iaa.studentmanagement.action.subjectActions.CreateSubjectAction"
            method="display">
        <result name="none">subjectFormCreate</result>
    </action>

我错过了什么吗?请告诉我。非常感谢, 李娜

0 个答案:

没有答案