1

我正在学习spring mvc。我开发了一个屏幕,在登录时会显示用户的个人数据。在同一屏幕上会有更新按钮来更新个人数据。

我的问题是,在personaldata.jsp 中,未在表单字段中提及的用户名、密码等用户属性在表单提交后设置为null。

下面是我的personaldata.jsp

  <form:form name="customerForm" id="customerForm" commandName="customerForm" method="post" >
            <form:hidden path="userName" id="userName"/>
             <form:hidden path="emailAddress" id="emailAddress"/>
             <form:hidden path="password" id="password"/>
            <div class="faq_rightin">
                <div class="faq_icon"><img src='<c:url value="/images/icon19.png" />' alt=""></div>
                <div class="faq_ohc2">
                    <h1><spring:message code="myaccount.label.personaldata" text="Personal Data"></spring:message></h1>
                    <span class="common ul-font-style m_top ">Welcome ${customerForm.firstName} ${customer.lastName}</span>
                    <span class="red"><spring:message code="myaccount.label.personalinfodesc" text="You may here enter your personal Information, enabling you to use those when you make your next booking. Doing so, your user experience is better, quicker."></spring:message></span>
                    <div class="m_top pd-form-box">
                        <div class="comm_md m_top float-left">
                            <div class="pd-colum1 mrg-rgt10">
                                <span class="pd-name-title mrg-rgt10">
                                    <select class="select-pd-name1" id="delivery_method" >
                                        <option class="select-pd-name1" value="0">Mr</option>
                                        <option value="1">Mrs</option>
                                        <option value="2">Miss</option>
                                    </select>
                                </span>
                                <span>
                                    <spring:message code="myaccount.label.firstname" text="First Name" var="firstname" />
                                    <form:input path="firstName" id="firstName" cssClass="pd-form-txt-common pd-txt1 float-left shadow"  placeholder="${firstname}*" />
                                </span>
                            </div>
                            <div class="pd-colum2 ">
                                <spring:message code="myaccount.label.familyname" text="Family Name" var="lastname"></spring:message>
                                <form:input path="lastName" id="lastName" cssClass="pd-form-txt-common pd-txt2 float-left shadow"  placeholder="${lastname}*" />
                            </div>
                        </div><!--end row -->

                        <div class="comm_md m_top float-left">
                            <div class="pd-colum1 mrg-rgt10">
                                <div class="comm_md float-left">
                                    <spring:message code="myaccount.label.emailaddress" text="Email-Address"></spring:message>* : ${customerForm.emailAddress}
                                </div>
                                <a href="#" class="comm_md blue_txt bold float-left"><spring:message code="myaccount.label.changeyouremailaddress" text="Change your Email Address"></spring:message> &gt;</a>                                
                            </div>
                            <div class="pd-colum2 ">
                                <div class="comm_md float-left">
                                    <spring:message code="myaccount.label.birthday" text="Birthday"></spring:message>*
                                </div>
                                <div class="comm_md float-left">
                                    <!--<form method="post" action="">  
                                        <div class="select_md3 float-left">
                                            <form:select path="day" id="" cssClass="select2">
                                                <form:option value="1">1</form:option>
                                                <form:option value="2">2</form:option>
                                                <form:option value="3">3</form:option>
                                                <form:option value="4">4</form:option>
                                            </form:select>
                                        </div>
                                    </form>-->
                                    <div class="select_md3 float-left">
                                        <form:select path="day" id="birthDay" cssClass="select2" items="${days}"/>
                                    </div>

                                    <span class="float-left f13 p_5">/</span>  
                                    <!--<form method="post" action="">  
                                        <div class="select_md3 float-left">
                                             <form:select path="month" id="" cssClass="select2">
                                                <form:option value="1">1</form:option>
                                                <form:option value="2">2</form:option>
                                                <form:option value="3">3</form:option>
                                                <form:option value="4">4</form:option>
                                            </form:select>
                                        </div>
                                    </form>-->
                                    <div class="select_md3 float-left">
                                        <form:select path="month" id="birthMonth" cssClass="select2" items="${months}"/>
                                    </div>
                                    <span class="float-left f13 p_5">/</span>
                                    <!--<form method="post" action="">  
                                        <div class="select_md4 float-left">
                                             <form:select path="year" id="" cssClass="select2">
                                                <form:option value="1">2011</form:option>
                                                <form:option value="2">2012</form:option>
                                                <form:option value="3">2013</form:option>
                                                <form:option value="4">2014</form:option>
                                             </form:select>
                                        </div>
                                    </form> -->
                                    <div class="select_md4 float-left">
                                        <form:select path="year" id="birthYear" cssClass="select2" items="${years}"/>
                                    </div>
                                </div>
                            </div>
                        </div><!--end row -->

                        <div class="comm_md m_top float-left">
                            <div class="pd-colum1 mrg-rgt10">
                                <span>
                                    <spring:message code="myaccount.label.code" text="Code" var="phonecode"></spring:message>
                                    <form:input path="phoneCode" id="phoneCode" cssClass="pd-form-txt-common pd-txt3 float-left mrg-rgt10 shadow" placeholder="${phonecode}"/>
                                </span>
                                <span>
                                    <spring:message code="myaccount.label.phonenumber" text="Phone Number" var ="phonenumber"></spring:message>
                                    <form:input path="phoneNumber" id="phoneNumber" cssClass="pd-form-txt-common pd-txt1 float-left shadow" placeholder="${phonenumber}"/>
                                </span>
                            </div>
                            <div class="pd-colum2 ">
                                <span>
                                    <spring:message code="myaccount.label.code" text="Code" var="mobileCode"></spring:message>
                                    <form:input path="mobileCode" id="mobileCode" cssClass="pd-form-txt-common pd-txt3 float-left mrg-rgt10 shadow" placeholder="${mobileCode}"/>
                                </span>
                                <span>
                                    <spring:message code="myaccount.label.mobilenumber" text="Mobile Number" var="mobileNumber"></spring:message>
                                    <form:input path="mobileNumber" id="mobileNumber" cssClass="pd-form-txt-common pd-txt4 float-left shadow"  placeholder="${mobileNumber}*"/>
                                </span>
                            </div>
                        </div><!--end row -->

                        <div class="comm_md m_top float-left">
                            <div class="pd-colum1 mrg-rgt10">
                                <form:select path="idDocument" cssClass="select-pd-name" id="idDocument">
                                    <form:option value="0"><spring:message code="myaccount.label.identificationdocument" text="Identification Document"></spring:message></form:option>
                                    <form:option value="1"><spring:message code="myaccount.label.passport" text="Passport"></spring:message></form:option>
                                    <form:option value="2"><spring:message code="myaccount.label.license" text="License"></spring:message></form:option>
                                </form:select>
                            </div>
                            <div class="pd-colum2 ">
                                <span class="f13 bold"><spring:message code="myaccount.label.invoiceaddressforcompanies" text="Invoice Address for Companies"></spring:message></span>
                            </div>
                        </div><!--end row -->

                        <div class="comm_md m_top float-left">
                            <div class="pd-colum1 mrg-rgt10">
                              <form:input path="idNumber" cssClass="pd-form-txt-common pd-txt5 float-left shadow" placeholder="Ausweisnumber*"/>
                            </div>
                            <div class="pd-colum2 ">
                                <input type="checkbox" name="" class="mrgrgt5" id=" "><span class="f13"><spring:message code="myaccount.label.sameasmailaddress" text="Same as mail Address"></spring:message> </span>
                            </div>
                        </div><!--end row -->

                        <div class="comm_md m_top float-left">
                            <div class="pd-colum1 mrg-rgt10">
                                <span class="f13 bold"><spring:message code="myaccount.label.address" text="Address"></spring:message></span>
                            </div>
                            <div class="pd-colum2 ">
                                <span>
                                    <spring:message code="myaccount.label.companyname" text="Company Name" var="companyname"></spring:message>
                                    <form:input id="companyName" path="companyName" class="pd-form-txt-common pd-txt2 float-left mrg-rgt10 shadow" placeholder="${companyname}"/>
                                </span>                       
                            </div>
                        </div><!--end row -->

                        <div class="comm_md m_top float-left">
                            <div class="pd-colum1 mrg-rgt10">
                                <span>
                                      <spring:message code="myaccount.label.street" text="Street" var="${street}"></spring:message>
                                      <form:input path="address.street" id="street" cssClass="pd-form-txt-common pd-txt1 float-left mrg-rgt10 shadow" placeholder="${street}"/>
                                </span>
                                <span>
                                    <form:input path="address.streetNo" id="streetNo" cssClass="pd-form-txt-common pd-txt3 float-left shadow" placeholder="No"/>
                                </span>
                            </div>
                            <div class="pd-colum2 ">                                
                                <span>
                                    <spring:message code="myaccount.label.street" text="Street" var="street"></spring:message>
                                    <form:input path="address.companyStreet" id="compStreet" cssClass="pd-form-txt-common pd-txt4 float-left mrg-rgt10 shadow" placeholder="${street}"/>
                                </span>
                                <span>
                                    <form:input path="address.companyStreetNo" id="compStreetNo" cssClass="pd-form-txt-common pd-txt3 float-left shadow" placeholder="No"/>
                                </span>
                            </div>
                        </div><!--end row -->

                        <div class="comm_md m_top float-left">
                            <div class="pd-colum1 mrg-rgt10">
                                <span>
                                    <spring:message code="myaccount.label.plz" text="PLZ" var="zip"></spring:message>
                                    <form:input path="address.postalCode" id="postalCode" cssClass="pd-form-txt-common pd-txt3 float-left mrg-rgt10 shadow" placeholder="${zip}"/>
                                </span>
                                <span>
                                    <spring:message code="myaccount.label.town" text="Town" var="compcity"></spring:message>
                                    <form:input path="address.city" id="city" cssClass="pd-form-txt-common pd-txt1 float-left shadow" placeholder="${compcity}"/>
                                </span>
                            </div>
                            <div class="pd-colum2 ">
                                <span>
                                    <spring:message code="myaccount.label.plz" text="PLZ" var="plzcomp"></spring:message>
                                    <form:input path="address.companyPostalCode" id="compPostalCode" cssClass="pd-form-txt-common pd-txt3 float-left mrg-rgt10 shadow" placeholder="${plzcomp}"/>
                                </span>
                                <span>
                                    <spring:message code="myaccount.label.town" text="Town" var="comtown"></spring:message>
                                    <form:input path="address.companyCity" id="compCity" cssClass="pd-form-txt-common pd-txt4 float-left shadow" placeholder="${comtown}"/>
                                </span>                         
                            </div>
                        </div><!--end row -->

                        <div class="comm_md m_top float-left">
                            <div class="pd-colum1 mrg-rgt10">
                                <!--<select class="select-pd-name" id="delivery_method" >
                                    <option class="select-pd-name" value="0"><spring:message code="myaccount.label.country" text="Country"></spring:message></option>
                                    <option value="1">England</option>
                                    <option value="2">Irland</option>
                                </select> -->
                                <spring:message code="myaccount.label.country" text="Country" var="country"></spring:message>
                                <form:input path="address.country" id="country" cssClass="select-pd-name" placeholder="${country}"/>
                            </div>
                            <div class="pd-colum2 ">
                                <!--  <select class="select-pd-name" id="delivery_method" >
                                    <option class="select-pd-name" value="0"><spring:message code="myaccount.label.country" text="Country"></spring:message></option>
                                    <option value="1">England</option>
                                    <option value="2">Irland</option>
                                </select>-->
                                <spring:message code="myaccount.label.country" text="Country" var="compCountry"></spring:message>
                                <form:input path="address.companyCountry" id="compCountry" cssClass="select-pd-name" placeholder="${compCountry}"/>
                            </div>
                        </div><!--end row -->                                               

                        <div class="comm_md m_top float-left">
                            <div class="pd-colum1 mrg-rgt10">
                                <select class="select-pd-name" id="delivery_method" >
                                    <option class="select-pd-name" value="0"><spring:message code="myaccount.label.eticketidentification" text="E-ticket Identification"></spring:message></option>
                                    <option value="1">England</option>
                                    <option value="2">Irland</option>
                                </select>
                            </div>
                            <div class="pd-colum2 flt-rgt ">
                                <spring:message code="myaccount.label.vatnumber" text="VAT Number" var="vatNumber"></spring:message>
                                <form:input path="vatNumber" id="vatNumber" cssclass="pd-form-txt-common pd-txt2 float-left mrg-rgt10 shadow" placeholder="${vatNumber}"/>
                            </div>
                        </div><!--end row -->

                        <div class="comm_md m_top float-left">
                            <div class="pd-colum1 mrg-rgt10">
                                <span class="f13 blue_txt"><b>*</b><spring:message code="myaccount.label.fieldsmustbefilledout" text="Fields must be filled out"></spring:message></span>
                            </div>
                            <div class="pd-colum2 flt-rgt ">
                                <input type="checkbox" name="" class="mrgrgt5"><span class="f13"><spring:message code="myaccount.label.futureinformationsofspecialofferings" text="I want to have in future informations about special of ferings for t4b customers."></spring:message> </span>
                            </div>
                        </div><!--end row -->

                        <div class="comm_md m_top float-left">
                            <div class="pd-colum1 mrg-rgt10">
                                <select class="select-pd-name" id="delivery_method" >
                                    <option class="select-pd-name" value="0"><spring:message code="myaccount.label.languagepreferred" text="Language ,Preferred"></spring:message></option>
                                    <option value="1">Sizilian</option>
                                    <option value="2">English</option>
                                </select>
                            </div>
                            <div class="pd-colum2 flt-rgt ">
                                <input type="submit" name="" id="store" class="common-btn float-right" value="<spring:message code="myaccount.label.store" text="Store"></spring:message>" />
                            </div>
                        </div><!--end row -->

                    </div><!--end pd-form-box -->                      
                </div>
            </div>
          </form:form>

为了解决这个问题,我必须对用户名和密码使用隐藏的表单字段。是否有任何其他解决方案使jsp中未提及的表单字段不设置为null?

下面是我成功登录后的请求处理程序

@RequestMapping(value = "/personaldata", method = RequestMethod.GET)
    public String personalData(Map<String, Object> map,
            HttpServletRequest request, Principal principal) {
        logger.info("Default Page");

        try {
            String userName = principal.getName();
            logger.debug("*******User name: " + userName);
            CustomerVO customerVO = customerService.findByUserName(userName);
            map.put("customerForm", customerVO);
        } catch (T4BException e) {
            logger.error(e.getLocalizedMessage());
        }
        /*
         * map.put("search", searchVO); if
         * (request.getParameter("ajax_get_standard_content") != null &&
         * request.getParameter("ajax_get_standard_content").equals("on")) {
         * return "home.definition.ajax"; } else { return "home.definition"; }
         */
        return "myaccount.personaldata";
    }

所以,我在模型 mao 中添加了我的支持 bean。

下面是我更新数据的请求处理程序

    @RequestMapping(value = "/storepersonal", method = RequestMethod.POST)
        public String storePersonal(@ModelAttribute("customerForm")CustomerVO customerVO , Map<String, Object> map,
                HttpServletRequest request, HttpSession session,Principal principal) {
        try {
            customerService.updateCustomer(customerVO);
            //map.put("customerForm", customerService.findByUserName(customerVO.getUserName()));
        } catch (T4BException e) {
            logger.error(e.getLocalizedMessage());
        }

    logger.info("Store Personal Page");
    /*
     * map.put("search", searchVO); if
     * (request.getParameter("ajax_get_standard_content") != null &&
     * request.getParameter("ajax_get_standard_content").equals("on")) {
     * return "home.definition.ajax"; } else { return "home.definition"; }
     */
    return "myaccount.personaldata";
} 

为 'customerForm' 指定的 @ModelAtrribute 应根据文档使用模型映射中存在的现有 modelAttribute。因此,只有 jsp 中提到的表单字段应该设置为用户输入的任何内容,customerForm 的其余字段应该与之前在模型映射中的状态相同。

4

1 回答 1

1

这是文档的摘录:

方法参数上的 @ModelAttribute 指示应从模型中检索该参数。如果模型中不存在,则应首先实例化参数,然后将其添加到模型中。一旦出现在模型中,参数的字段应该从具有匹配名称的所有请求参数中填充。

[...]

@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute Pet pet) {

}

给定上面的例子, Pet 实例从哪里来?有几种选择:

  • 由于使用了@SessionAttributes,它可能已经在模型中——请参阅“使用@SessionAttributes 在请求之间的HTTP 会话中存储模型属性”一节。
  • 由于同一控制器中的 @ModelAttribute 方法,它可能已经在模型中——如上一节所述。
  • 它可以基于 URI 模板变量和类型转换器来检索(下面将更详细地解释)。
  • 它可以使用其默认构造函数进行实例化。

所以你的控制器应该有以下内容:

@ModelAttribute("foo")
private Foo prepareFoo() {
    // get the foo to update from the database and return it.
    // Spring will store it in the model
}

@RequestMapping(...)
public ModelAndView submitForm(@ModelAttribute("foo") Foo foo) {
    // Spring will pass the Foo that has been prepared and stored in the model,
    // updated with the values submitted in the HTML form
}
于 2013-05-31T07:11:34.943 回答