182云笔记项目.docx

上传人:b****4 文档编号:4307202 上传时间:2022-11-29 格式:DOCX 页数:13 大小:183.54KB
下载 相关 举报
182云笔记项目.docx_第1页
第1页 / 共13页
182云笔记项目.docx_第2页
第2页 / 共13页
182云笔记项目.docx_第3页
第3页 / 共13页
182云笔记项目.docx_第4页
第4页 / 共13页
182云笔记项目.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

182云笔记项目.docx

《182云笔记项目.docx》由会员分享,可在线阅读,更多相关《182云笔记项目.docx(13页珍藏版)》请在冰豆网上搜索。

182云笔记项目.docx

182云笔记项目

Ø登录(续)

显示错误消息

原理:

1.重构控制器增加异常处理方法UserController

@ExceptionHandler(UserNotFoundException.class)

@ResponseBodypublicJsonResulthandleUserNotFound(UserNotFoundExceptione){e.printStackTrace();returnnewJsonResult(2,e);}

@ExceptionHandler(PasswordException.class)

@ResponseBodypublicJsonResulthandlePassword(PasswordExceptione){e.printStackTrace();returnnewJsonResult(3,e);}

2.重构JsonResult添加构造器

publicJsonResult(intstate,Throwablee){this.state=state;this.message=e.getMessage();}

3.重构login.js的loginAction方法,显示错误信息

varmsg=result.message;

if(result.state==2){$('#count').next().html(msg);

}elseif(result.state==3){

$('#password').next().html(msg);

}else{alert(msg);}

4.测试

注册功能

原理:

1.持久层

1.声明持久层方法:

UserDao

intaddUser(Useruser);

2.声明SQLUserMappeer.xml

insertintocn_user(cn_user_id,cn_user_name,cn_user_password,cn_user_token,cn_user_nick)values(#{id},#{name},#{password},#{token},#{nick})

3.测试UserDaoTest:

UserDaodao;

@BeforepublicvoidinitDao(){

dao=ctx.getBean("userDao",UserDao.class);}

@Test

publicvoidtestAddUser(){

Stringid=UUID.randomUUID().toString();

Stringname="Tom";Stringsalt="今天你吃了吗?

";

Stringpassword=DigestUtils.md5Hex(salt+"123456");

Stringtoken="";Stringnick="";

Useruser=newUser(id,name,password,token,nick);

intn=dao.addUser(user);

System.out.println(n);}

2.业务层

1.声明业务层方法UserService

/***UserService中添加注册功能

*@paramname*@paramnick*@parampassword*@paramconfirm*@return注册成功的用户信息*@throwsUserNameException用户名异常

*@throwsPasswordException密码异常

*/Userregist(Stringname,Stringnick,Stringpassword,Stringconfirm)throwsUserNameException,PasswordException;

2.声明业务层异常UserNameException

publicclassUserNameExceptionextendsRuntimeException{privatestaticfinallongserialVersionUID=6435296194529486206L;publicUserNameException(){}publicUserNameException(Stringmessage){super(message);}publicUserNameException(Throwablecause){super(cause);}publicUserNameException(Stringmessage,Throwablecause){super(message,cause);}publicUserNameException(Stringmessage,Throwablecause,booleanenableSuppression,booleanwritableStackTrace){super(message,cause,enableSuppression,writableStackTrace);}}

3.重构jdbc.properties和UserServiceImpl,将salt存储到配置文件,利用Spring注入到属性中:

#jdbc.propertiessalt=\u4ECA\u5929\u4F60\u5403\u4E86\u5417?

//UserServiceImpl@Value("#{jdbc.salt}")privateStringsalt;

4.实现业务层方法:

UserServiceImpl

publicUserregist(Stringname,Stringnick,Stringpassword,Stringconfirm)throwsUserNameException,PasswordException{

//检查name,不能重复

if(name==null||name.trim().isEmpty()){thrownewUserNameException("不能空");}

Userone=userDao.findUserByName(name);

if(one!

=null){thrownewUserNameException("已注册");}

//检查密码

if(password==null||password.trim().isEmpty()){thrownewPasswordException("不能空");}

if(!

password.equals(confirm)){thrownewPasswordException("确认密码不一致");}

//检查nickif(nick==null||nick.trim().isEmpty()){nick=name;}

Stringid=UUID.randomUUID().toString();Stringtoken="";

password=DigestUtils.md5Hex(salt+password);

Useruser=newUser(id,name,password,token,nick);

intn=userDao.addUser(user);

if(n!

=1){thrownewRuntimeException("添加失败!

");}returnuser;}

5.测试:

UserServiceTest

UserServiceservice;

@Before

publicvoidinitService(){service=ctx.getBean("userService",UserService.class);}

@Test

publicvoidtestRegist(){Useruser=service.regist("Andy","Andy","123456","123456");System.out.println(user);}

3.控制器

1.添加控制器方法UserController

@RequestMapping("/regist.do")

@ResponseBody

publicJsonResultregist(Stringname,Stringnick,Stringpassword,Stringconfirm){

Useruser=userService.regist(name,nick,password,confirm);

returnnewJsonResult(user);}

2.测试

http:

//localhost:

8080/note/user/regist.do?

name=Jerry&nick=AN&password=12345&confirm=12345

4.添加注册JS脚本

1.更新log_in.html取消页面检查js脚本:

window.onload=function(){

vart=setTimeout("get('zc').style.visibility='visible'",800);//get('final_password').onblur=function(){

//varnpassword=get('regist_password').value;

//varfpassword=get('final_password').value;

//if(npassword!

=fpassword){

//get('warning_3').style.display='block';//}//}//get('regist_password').onblur=function(){

//varnpassword=get('regist_password').value.length;

//if(npassword<6&&npassword>0){

//get('warning_2').style.display='block';//}//}//get('regist_password').onfocus=function(){//get('warning_2').style.display='none';//}//get('final_password').onfocus=function(){//get('warning_3').style.display='none';//}}

2.添加注册对话框事件脚本login.js

$('#regist_button').click(registAction);

$('#regist_username').blur(checkRegistName);$('#regist_password').blur(checkRegistPassword);

$('#final_password').blur(checkConfirm);

3.添加注册对话框数据检验方法:

login.js

functioncheckConfirm(){varpwd2=$('#final_password').val();

varpwd=$('#regist_password').val();

//pwd如果是空值表示false,非空则是true

if(pwd&&pwd==pwd2){

$('#final_password').next().hide();returntrue;}$('#final_password').next().show().find('span').html('确认密码不一致');returnfalse;}functioncheckRegistPassword(){

varpwd=$('#regist_password').val().trim();

varrule=/^\w{4,10}$/;

if(rule.test(pwd)){$('#regist_password').next().hide();returntrue;}$('#regist_password').next().show().find('span').html('4~10个字符');returnfalse;}functioncheckRegistName(){

varname=$('#regist_username').val().trim();

varrule=/^\w{4,10}$/;

if(rule.test(name)){$('#regist_username').next().hide();returntrue;}$('#regist_username').next().show().find('span').html('4~10字符');returnfalse;}

4.添加注册对话框注册按钮事件方法login.js

functionregistAction(){console.log('registAction');

//检验界面参数varn=checkRegistName()+checkRegistPassword()+checkConfirm();if(n!

=3){return;}

//获取界面中表单数据

varname=$('#regist_username').val().trim();

varnick=$('#nickname').val();varpassword=$('#regist_password').val();

varconfirm=$('#final_password').val();

//发起AJAX请求

varurl='user/regist.do';

vardata={name:

name,nick:

nick,password:

password,confirm:

confirm};

//console.log(data);

//$.post是$.ajax的简化版

$.post(url,data,function(result){console.log(result);

if(result.state==0){//退回登录界面$('#back').click();

varname=result.data.name;

$('#count').val(name);$('#password').focus();

//清空表单$('#regist_username').val('');

$('#nickname').val('');$('#regist_password').val('');

$('#final_password').val('');

}elseif(result.state==4){$('#regist_username').next().show().find('span').html(result.message);}elseif(result.state==3){$('#regist_password').next().show().find('span').html(result.message);}else{alert(result.message);}});//得到响应以后,更新界面}

5.重构控制器UserController增加事件处理方法:

@ExceptionHandler(UserNameException.class)

@ResponseBodypublicJsonResulthandleUserName(UserNameExceptione){e.printStackTrace();returnnewJsonResult(4,e);}

6.测试...

调试工具:

Ø笔记本列表功能

原理:

1.持久层

1.添加持久层接口NotebookDao:

publicinterfaceNotebookDao{List>findNotebooksByUserId(StringuserId);}

2.添加Mapper文件:

NotebookMapper.xml

selectcn_notebook_idasid,cn_notebook_nameasnamefromcn_notebookwherecn_user_id=#{userId}orderbycn_notebook_createtimedesc

3.测试:

publicclassNotebookDaoTestextendsBaseTest{NotebookDaodao;@BeforepublicvoidinitDao(){dao=ctx.getBean("notebookDao",NotebookDao.class);}@Test//selectcn_user_idfromcn_notebook;publicvoidtestFindNotebooksByUserId(){StringuserId="52f9b276-38ee-447f-a3aa-0d54e7a736e4";List>list=dao.findNotebooksByUserId(userId);for(Mapmap:

list){System.out.println(map);}}}

提示:

需要先到数据库中获得拥有笔记的用户IDuserId

selectcn_user_idfromcn_notebook;

List封装查询结果:

2.业务层

1.声明业务接口NotebookService

publicinterfaceNotebookService{List>listNotebooks(StringuserId)throwsUserNotFoundException;}

2.实现业务接口NotebookServiceImpl

@Service("notebookService")publicclassNotebookServiceImplimplementsNotebookService{@ResourceprivateNotebookDaonotebookDao;@ResourceprivateUserDaouserDao;publicList>listNotebooks(StringuserId)throwsUserNotFoundException{if(userId==null||userId.trim().isEmpty()){thrownewUserNotFoundException("ID不能空");}Useruser=userDao.findUserById(userId);if(user==null){thrownewUserNotFoundException("用户不存在");}returnnotebookDao.findNotebooksByUserId(userId);}}

3.添加查询方法UserDao

UserfindUserById(StringuserId);

4.添加SQLUserMapper

selectcn_user_idasid,cn_user_nameasname,cn_user_passwordaspassword,cn_user_tokenastoken,cn_user_nickasnickfromcn_userwherecn_user_id=#{userId}

5.测试:

publicclassNotebookServiceTestextendsBaseTest{NotebookServiceservice;@BeforepublicvoidinitService(){service=ctx.getBean("notebookService",NotebookService.class);}@TestpublicvoidtestListNotebooks(){StringuserId="52f9b276-38ee-447f-a3aa-0d54e7a736e4";List>list=service.listNotebooks(userId);for(Mapmap:

list){System.out.println(map);}}}

Ø笔记本列表

3.控制器

1.添加控制器父类AbstractController,封装公共的异常处理方法:

publicabstractclassAbstractController{/***在其他控制器方法执行出现异常时候,执行*异常处理方法handleException*/@ExceptionHandler(Exception.class)@ResponseBodypublicObjecthandleException(Exceptione){e.printStackTrace();returnnewJsonRes

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1