一个XML中只能有父节点<beans>
ctrl+shift+o 对导入的包进行处理,ctrl+shift+f 把格式重新排布
变量首字母要小写,其后首字母大写:userId。定义静态变量时才用到下划线:USER_ID
<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>