如题,其实映射关系很简单,就是jsp页面的表单中的name属性,要与bean实体类的属性名对应。
举个例子,JSP页面代码:
<form action="${pageContext.request.contextPath}/user/login.do"
method="post" οnsubmit="return checkForm()">
<h1>Login</h1>
<div>
<input type="text" placeholder="用户名" required="" id="username2" name="username" value="${user.username }"/>
</div>
<div>
<input type="password" placeholder="密码" required="" id="password2" name="password3" value="${user.password }"/>
</div>
<div class="sure">
<input type="text" placeholder="验证码" required="" id="verificationcode" name="verificationcode"/>
<button type="button" class="btn btn-md btn-warning change" οnclick="changeCode()">换一张</button>
<img src="${pageContext.request.contextPath}/code/captcha-image.do" id="kaptchaImage" class="img-sty"/>
</div>
<div>
<input type="submit" value="登 录" style="width: 92%;"/>
</div>
</form>
后台bean代码:
public class User {
private Integer id;
private String username;
private String password;
private String realname;
private String userArea;
private Integer userType;
private Integer userState;
private Date updateTime;
private Date createTime;
private String roleIds;
/* 相应的getter,setter方法省略 */
}
如上,jsp的表单中,username是可以正常映射的,但是 password就不行,因为name属性对应不上。
另外,补充一点,据说在struts2框架中,这种映射关系有点不一样,如果是在struts2中,name属性要写成类似:
<div>
<input type="text" placeholder="用户名" required="" name="user.username" />
</div>
这样才能获取得到哦。