1.用户唯一性校验细节
ajax后台异步校验
function doVerify(){
var account = $('#account').val();
if(account!=""){
$.ajax({
url:"${basePath}ssh/user_verifyAccount.action",
data:{"user.account":account},
type:"post",
async:false,
success:function(msg){
if("true" != msg){
alert("账号已经存在");
isUsed=true;
}else{
isUsed=false;
}
}
});
}
}
(1)ajax基本属性:
$.ajax({
url:"完整url",
data:{"json类型数据":args},
type:"post",
async:false,
success:function(msg){}
});
(2)对应Action实现不需要写返回值,即使是struts
public void verifyAccount(){}
(3)返回text类型数据给前台ajax做法,judge是查询后台是否有重复数据
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
out.write(judge.getBytes());
out.close();
(4)编辑页面校验影响解决方式(问题:用户必须修改账号才可提交)
public List<User> findUserByAccountAndId(String id, String account) {
String hql = "from User where account = ?0 ";
Query query;
//重点逻辑:前台传值id不为空添加搜索字段id
if(StringUtils.isNotBlank(id)){
/*有点难理解,在编辑时数据库内的account和id与传来的值不对应的数据条目是不存在的。
* 因为存在的话证明有其他id使用了相同account*/
hql += "and id != ?1";
query = currentSession().createQuery(hql).setParameter(0, account).setParameter(1, id);
}else{
query = currentSession().createQuery(hql).setParameter(0, account);
}
return query.list();
}
2.全局异常映射尝试
(1)自建package+异常类型
·让struts其他package继承
· 当抛出指定异常类时可以根据映射结果集跳转到指定页面显示异常(自定义输出内容)
·暂时感觉不到有什么实用性,myEclipse的报错更容易看懂- -
<package name="base-default" extends="struts-default">
<!-- 全局返回值映射 -->
<global-results>
<result name="sysError">/WEB-INF/jsp/error.jsp</result>
</global-results>
<!-- 抛出exception指定异常类时返回result字段 -->
<global-exception-mappings>
<exception-mapping
result="sysError"
exception="com.vincent.core.exception.SysException">
</exception-mapping>
</global-exception-mappings>
</package>
自定义异常父类
package com.vincent.core.exception;
public class SysException extends Exception {
public SysException() {
super("出现异常");
// TODO Auto-generated constructor stub
}
public SysException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
}
自定义业务层异常类
package com.vincent.core.exception;
public class ServiceException extends SysException {
private String errMsg;
public ServiceException() {
super();
// TODO Auto-generated constructor stub
}
public ServiceException(String message) {
super(message);
errMsg = message;
// TODO Auto-generated constructor stub
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
}
3.角色权限关系设置(神奇的多对多关联设置法)
角色:实体类
public class Role implements java.io.Serializable {
// Fields
private String roleId;
private String name;
private String state;
//多对多映射准备
private Set<RolePrivilege> rolePrivilges;
权限:字段?!!!
/*--------------------权限集合------------------------*/
public static String PRIVILGE_XZGL = "xzgl";
public static String PRIVILGE_HQFW = "hqfw";
public static String PRIVILGE_ZXXX = "zxxx";
public static String PRIVILGE_NSFW = "nsfw";
public static String PRIVILGE_SPACE = "space";
做法:传统多对多要中间表和双方映射文件配置many-to-many,但现在其中一方没有实体类,反思类与中间表的关系:
角色1-n中间表n-1权限
所以只要实现了角色与中间表的1-n映射就够了。n方使用联合主键作为one-to-many的外键,联合主键又角色id与权限字段共同组成一个唯一的约束(大概是这样的意思- -,角色有很多,权限也有很多,但角色+权限是唯一的)
So:重点是掌握联合主键的写法
联合主键类+hibernate配置
要建表的类:相当于中间表的角色,所以要有配置文件
public class RolePrivilege implements Serializable{
private RolePrivilegeId id;
主键类:被上面的类引用,包含角色类(方便以角色获取权限*)+权限字段
public class RolePrivilegeId implements Serializable{
private Role role;
private String code;
hibernate配置文件:
一方配置文件:
测试:
public String add(){
if(role != null){
Set<RolePrivilege> set = new HashSet<RolePrivilege>();
//前台传来的是权限字段集合,判断是否为空
if(privilegesIds != null){
//手动创建添加中间表类并加入内容
for(int i=0;i<privilegesIds.length;i++){
set.add(new RolePrivilege(new RolePrivilegeId(role,privilegesIds[i])));
}
//这里往后和一般的级联操作没区别
role.setRolePrivilges(set);
}
roleService.save(role);
}
return "returnList";
}
结果: