gpt4 book ai didi

java - Struts2 验证失败时重新填充表单信息

转载 作者:行者123 更新时间:2023-12-01 04:32:44 25 4
gpt4 key购买 nike

我有一个页面,其中包含一个预先填充了用户信息的表单。这是一个用户个人资料页面。我对某些字段进行了验证,但目前我的验证方法只是硬编码来执行 addFieldError("exp", "Cats");哪里exp是一个正在验证的变量,Cats 是一条随机消息。该表单具有选择和双重选择,我通过在 jsp 中执行操作来重新填充它们。 (见下文)

这是完整的表格:

<s:form action="%{formAction}" method="post" enctype="multipart/form-data">
<div id="buttons">
<s:submit value="Save" />
</div>

<div id="left_column" class="divStyle">
<div id="picture">
<div id="picture_border">
Picture should go here 150px x 150px
</div>
<s:file name="upload" label="File" />
</div>

<hr />

<div id="contact" class="divPad">
<h3>Contact Information</h3>
<s:textfield name="email" value="%{profile.email}" required="true" />
</div>

<hr />

<div id="availabilityDuration" class="divPad">
<h3>When Available</h3>
<s:textfield name="whenAvailable" value="%{profile.whenAvailable}" />

<h3>Availability Length</h3>
<s:textfield name="availabilityLength" value="%{profile.availabilityLength}" />

<h3>Desired Credit</h3>
<s:action name="CreditSelectAction" executeResult="true" />
</div>
</div>


<div id="right_column" class="divStyle">
<div id="basic_info" class="divPad">
<h4>College & Major</h4>
<s:action name="CollegeMajorsSelectAction" executeResult="true" />
<hr />
<h4>Years of Work Experience</h4>
<s:action name="ExpYearsSelectAction" executeResult="true" /> <hr />
<h4>Undergrad</h4>
<s:action name="UndergradSelectAction" executeResult="true" /> <hr />
<h4>Graduation Year</h4>
<s:action name="GradYearSelectAction" executeResult="true" />
</div>

<hr />

<div id="aboutDescription" class="divPad">
<h3>About Me</h3>
<s:textarea name="comments" value="%{profile.comments}" cols="40" rows="10" />
</div>

<hr />

<div id="skillsNeeds" class="divPad">
<h3>Skills</h3>
<div id="userSkillList">
<s:iterator value="profile.skills" status="status">
<div>
<h5 class="formLabels">Skill Description</h5>
<s:textfield name="userSkillDescription_%{#status.count}" value="%{description}" />

<h5 class="formLabels">Years of Experience</h5>
<s:textfield name="userSkillExperience_%{#status.count}" value="%{experience}"/>

<h5 class="removeSkillLink" onclick="removeUserSkill(this);">Remove Skill</h5>
</div>
</s:iterator>
<h5 class="addSkillLink" id="addSkill" onclick="addUserSkill();">Add New Skill</h5>
</div>
</div>

</div>
</s:form>

下拉菜单填充正常。问题是,当我重新加载 jsp 时,我认为会保存在值堆栈中并在重新加载 jsp 时保留的值( %{formAction}%{profile.email} 等)不会被保留。如何捕获这些值并在验证失败后重新加载页面时显示它们?我尝试将它们添加到 session 中,但这往往会变得困惑,而且我不确定如何使其与 formAction 一起使用。 .

struts.xml 中的代码:

<action name="updateProfile" class="profiles.actions.UpdateProfileAction" method="execute">
<!-- <interceptor-ref name="basicStack"/>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg,image/gif,image/png,image/jpg</param>
</interceptor-ref>
<interceptor-ref name="validation"/>
<interceptor-ref name="workflow"/> -->
<result name="success">/index.jsp</result>
<result name="input">/jsp/editProfile.jsp</result>
</action>

加载表单的操作中的代码片段:

public String execute()
{

// get the user profile
String result = "success";

//If the profile is null, then the user is new and does not yet have a profile
//NOTE: If the user's profile doesn't exist, when trying to view someone else's
//profile, they will be redirected to edit their own.
if(user.intValue() == 0)
{
logger.info("New User Detected. Returning Failure.");
result = "failure";
}
else
{
//If the userid is null, we are loading the user's profile
//Otherwise, we are viewing someone else's profile

if(userid == null)
userid = user.toString();

profile = dao.selectCurUserById(Integer.parseInt(userid));

// get all of my projects
this.setMyProjects(projectDAO.selectMyProjects(Integer.parseInt(userid)));

// get all of the projects i've been invited to
this.setJoinedProjects(projectDAO.selectJoinedProjects(Integer.parseInt(userid)));
}
return result;

}

更新用户个人资料的操作的代码片段:

public String execute()
{
// request that sent from the client
HttpServletRequest request = ServletActionContext.getRequest();
Map<String, Object> session = ActionContext.getContext().getSession();

profile = new UserProfile();
id = ((authentication.beans.User) session.get("currentUser")).getId();
profile.setId(id);
profile.setEmail(email);
profile.setAvailabilityLength(availabilityLength);
profile.setComments(comments);
profile.setUndergrad(Integer.parseInt(undergrad));
profile.setWhenAvailable(whenAvailable);
profile.setYear(year);
profile.setCredit(credit);
profile.setMajor(Major.getMajor(major));
profile.setCollege(College.getCollege(college));
profile.setExp(exp);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy");
profile.setModify(sdf.format(new GregorianCalendar().getTime()));

//If file is not null, then the user is uploading an image for his/her
//profile
if(file != null)
{
byte[] b = new byte[(int) file.length()];
try
{
new FileInputStream(file).read(b);
b = Base64.encode(b);
}
catch (IOException e)
{
// TODO Auto-generated catch block
profile.setComments(profile.getComments() + "\n" + e.getMessage());
}
profile.setPic(b);
}

// get all of the params and then process the user skills
HashMap<String,String[]> params = (HashMap<String,String[]>)request.getParameterMap();

// process the user skills
profile.setSkills(processSkills(params));

// add the userid to the skills
for(int i = 0; i < profile.getSkills().size(); i++){
profile.getSkills().get(i).setUserID(profile.getId());
}

//TODO: Check the result and do error handling
boolean result = dao.updateProfile(profile);

return "success";
}

更新

问题几乎就是coding_idiot所说的。在加载表单的操作中,我需要有 getter 来获取信息以最初填充表单(有这个)。在更新信息的操作中,我需要将信息放入表单中的 setter 和 getter ,以便在验证失败后应重新填充表单时从何处获取新信息。我通过填充 profile 解决了这个问题validate() 中的对象使用我从表单获取的数据在更新操作中使用函数,然后为 profile 提供 setter 目的。我不必对我的 .jsp 进行任何更改

最佳答案

我不知道你在action中写了什么,但是从上面我可以推断这是对功能问题的一个轻微误解。您有带有名称的字段 - 电子邮件、whenAvailable 等,但它们的值是从配置文件对象中获取的。

你怎么能期望用一个不存在的东西来设置它呢?

To make it more clear : 
Values that go from JSP to action is email, but what comes back is profile.email, instead it
should have been just email, or you can simply skip the value attribute or you can change your
field-name to profile.email

请告诉我这是否有帮助。

[编辑]

再次阅读你的问题,配置文件的 getter 和 setter 没有帮助,因为什么进入的操作只是原始字段(电子邮件等),因此您应该为这些字段设置 getter/setter,然后将这些字段重新填充到 JSP 中您可以简单地用于例如:

 <s:textfield name="whenAvailable" value="%{whenAvailable}" />

或者

 <s:textfield name="whenAvailable"/>

关于java - Struts2 验证失败时重新填充表单信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17789964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com