7月20号总结

  1. 一个XML中只能有父节点<beans>

  2. ctrl+shift+o   对导入的包进行处理,ctrl+shift+f 把格式重新排布

  3. 变量首字母要小写,其后首字母大写:userId。定义静态变量时才用到下划线:USER_ID

  4. <span    th:text

        <input    th:value

5、实现数据更新及显示错误信息

一、配置:

1、在src中添加:ValidationMessages.properties

errors.required={field}\u4e3a\u5fc5\u987b\u8f93\u5165\u9879\u76ee

2、在cn.agriculture.common.validator.constraints包中定义两个类

NotEmpty.java

package cn.agriculture.common.validator.constraints;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;

import static java.lang.annotation.ElementType.CONSTRUCTOR;

import static java.lang.annotation.ElementType.FIELD;

import static java.lang.annotation.ElementType.METHOD;

import static java.lang.annotation.ElementType.PARAMETER;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

import javax.validation.Constraint;

import javax.validation.Payload;

import cn.agriculture.common.validator.constraints.NotEmpty;

import cn.agriculture.common.validator.constraints.NotEmptyValidator;

@Retention(RetentionPolicy.RUNTIME)  

@Target( { ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER })  

@Constraint(validatedBy = { NotEmptyValidator.class })  

public @interface NotEmpty {  

String field() default "";  

    String message() default "{org.hibernate.validator.constraints.NotEmpty.message}";  

Class<?>[] groups() default { };

Class<? extends Payload>[] payload() default { };

@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })

@Retention(RUNTIME)

@Documented

public @interface List {

NotEmpty[] value();

}

}

NotEmptyValidator.java

package cn.agriculture.common.validator.constraints;

import javax.validation.ConstraintValidator;

import javax.validation.ConstraintValidatorContext;

import cn.agriculture.common.validator.constraints.NotEmpty;

public class NotEmptyValidator implements ConstraintValidator<NotEmpty, CharSequence> {     

public void initialize(NotEmpty annotation) {

}

/**

* Checks that the trimmed string is not empty.

*

* @param charSequence The character sequence to validate.

* @param constraintValidatorContext context in which the constraint is evaluated.

*

* @return Returns <code>true</code> if the string is <code>null</code> or the length of <code>charSequence</code> between the specified

*         <code>min</code> and <code>max</code> values (inclusive), <code>false</code> otherwise.

*/

public boolean isValid(CharSequence charSequence, ConstraintValidatorContext constraintValidatorContext) {

if ( charSequence == null ) {

return true;

}

return charSequence.toString().trim().length() > 0;

}

}

3、在lib中导入包 validation-api-1.1.0.Final.jar

4、在UserB中加入@NotEmpty(field="用户密码",  message="{errors.required}")

              @NotEmpty(field="用户姓名",  message="{errors.required}")

二、编写更新代码

1、  <td ><a th:href="@{updateName(userid=${yonghu.userid})}"><span th:text="${yonghu.username}">12</span></a></td> 点击跳到更新页面           //虽然现在是点击用户名跳转,但还应该通过ID来查找用户信息!

2、在HelloController中

    //2更新跳转

    @RequestMapping(value = "updateName", method = RequestMethod.GET)

    public String updatename(UserB userB, Model model) {

    UserB result=helloWorldService.selectnameid(userB);

    model.addAttribute("UserB",result);  

       return "update";

}

    //更新代码

    @RequestMapping(value = "/updateUserName", method = RequestMethod.POST)

  public String updateyonghu(HttpServletRequest req,@Valid @ModelAttribute("userForm") UserB UserB, BindingResult results,Model model) throws SQLException {

   String repassword = req.getParameter("repassword");

   String password = req.getParameter("password");

   String username=req.getParameter("username");

   if(username.isEmpty())

   {

model.addAttribute("message", "用户名不能为空!");

UserB result=helloWorldService.selectnameid(UserB);

      model.addAttribute("UserB",result);  

return "update";

   }

   if(password.isEmpty())

   {

model.addAttribute("message", "密码不能为空");

UserB result=helloWorldService.selectnameid(UserB);

     model.addAttribute("UserB",result);  

return "update";

   }

   if(!UserB.getPassword().equals(repassword))

   {

  log.info("密码验证出错");

model.addAttribute("message", "密码和密码确认必须一致!");

UserB result=helloWorldService.selectnameid(UserB);

     model.addAttribute("UserB",result);  

return "update";

   }

   log.info("修改客户信息");

      if(password.equals(repassword)){

      if(helloWorldService.updateYonghu(UserB)!=0)

      {

      UserB result=helloWorldService.selectid(UserB);

      model.addAttribute("UserB",result);  

      List<UserB> result1=helloWorldService.allyonghu(UserB);

     model.addAttribute("list",result1);

  return "meeage";

  }

  else

  return "update";

      }

      else

      {

      return "update";

      }

  }

3、在HelloService中

public int updateYonghu(UserB uu){

return updateDao.execute("User.updateYonghu",uu);

}

public UserB selectnameid(UserB ss){

UserB result=queryDao.executeForObject("User.selectnameid",ss, UserB.class);

return result;

}

}

4、在serSQLMap中

<select id="selectid"

parameterClass="cn.training.controller.UserB"

resultClass="cn.training.controller.UserB">

SELECT

userid

FROM

yonghu

WHERE

userid= #userid#

</select>

<select id="selectnameid"

parameterClass="cn.training.controller.UserB"

resultClass="cn.training.controller.UserB">

SELECT

userid,username,password

FROM

yonghu

WHERE

userid=#userid#

</select>

<update id="updateYonghu"

parameterClass="cn.training.controller.UserB">

UPDATE  yonghu SET username=#username#,password=#password# WHERE userid= #userid#

</update>

</sqlMap>


转载于:https://my.oschina.net/u/2413996/blog/480961

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值