ImageVerifierCode 换一换
格式:DOCX , 页数:12 ,大小:19.86KB ,
资源ID:7226038      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/7226038.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Spring 注解学习手札三 表单页面处理.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Spring 注解学习手札三 表单页面处理.docx

1、Spring 注解学习手札三 表单页面处理如果要说表单,最简单的就是用户登录页面了!估计大多数做B/S出身的兄弟可能写的第一个表单就是登录表单了!今天,我也不例外,做一个登录验证实现!首先,改造一下账户类Account,增加一个id字段:Account.javaJava代码1. /*2. *2010-1-233. */4. packageorg.zlex.spring.domain;5. 6. importjava.io.Serializable;7. 8. /*9. *账户10. *11. *author梁栋12. *version1.013. *since1.014. */15. publ

2、icclassAccountimplementsSerializable16. 17. /*18. *19. */20. privatestaticfinallongserialVersionUID=-533698031946372178L;21. 22. /*23. *主键24. */25. privateintid;26. /*27. *用户名28. */29. privateStringusername;30. /*31. *密码32. */33. privateStringpassword;34. 35. 36. 37. publicAccount()38. 39. 40. /*41.

3、 *paramid42. */43. publicAccount(intid)44. this.id=id;45. 46. 47. /get、set方法省略48. 49. 接下来,为了协调逻辑处理,我们改造接口AccountService及其实现类AccountServiceImpl:AccountService.javaJava代码1. /*2. *2010-1-233. */4. packageorg.zlex.spring.service;5. 6. importorg.springframework.transaction.annotation.Transactional;7. imp

4、ortorg.zlex.spring.domain.Account;8. 9. /*10. *账户业务接口11. *12. *author梁栋13. *version1.014. *since1.015. */16. Transactional17. publicinterfaceAccountService18. 19. /*20. *获得账户21. *22. *paramusername23. *parampassword24. *return25. */26. Accountread(Stringusername,Stringpassword);27. 28. /*29. *获得账户30

5、. *31. *paramid32. *return33. */34. Accountread(intid);35. 我们暂时抛开AccountDao该做的事情,在AccountServiceImpl中完成数据提取:AccountServiceImpl.javaJava代码1. /*2. *2010-1-233. */4. packageorg.zlex.spring.service.impl;5. 6. importorg.springframework.beans.factory.annotation.Autowired;7. importorg.springframework.stere

6、otype.Service;8. importorg.zlex.spring.dao.AccountDao;9. importorg.zlex.spring.domain.Account;10. importorg.zlex.spring.service.AccountService;11. 12. /*13. *账户业务14. *15. *author梁栋16. *version1.017. *since1.018. */19. Service20. publicclassAccountServiceImplimplementsAccountService21. 22. Autowired2

7、3. privateAccountDaoaccountDao;24. 25. Override26. publicAccountread(Stringusername,Stringpassword)27. Accountaccount=null;28. if(username.equals(snowolf)&password.equals(zlex)29. account=newAccount();30. account.setId(1);31. account.setUsername(username);32. account.setPassword(password);33. 34. re

8、turnaccount;35. 36. 37. Override38. publicAccountread(intid)39. Accountaccount=newAccount();40. account.setId(1);41. account.setUsername(snowolf);42. account.setPassword(zlex);43. returnaccount;44. 45. 先来一个账户信息的展示,构建一个控制器ProfileController:ProfileController.javaJava代码1. /*2. *2010-1-263. */4. package

9、org.zlex.spring.controller;5. 6. importorg.springframework.beans.factory.annotation.Autowired;7. importorg.springframework.stereotype.Controller;8. importorg.springframework.ui.ModelMap;9. importorg.springframework.web.bind.annotation.RequestMapping;10. importorg.springframework.web.bind.annotation.

10、RequestMethod;11. importorg.springframework.web.bind.annotation.RequestParam;12. importorg.zlex.spring.domain.Account;13. importorg.zlex.spring.service.AccountService;14. 15. /*16. *账户信息控制器17. *18. *author梁栋19. *version1.020. *since1.021. */22. Controller23. RequestMapping(value=/profile.do)24. publ

11、icclassProfileController25. Autowired26. privateAccountServiceaccountService;27. 28. /*29. *账户信息展示30. *31. *paramid32. *parammodel33. *return34. */35. RequestMapping(method=RequestMethod.GET)36. publicStringprofile(RequestParam(id)intid,ModelMapmodel)37. Accountaccount=accountService.read(id);38. mo

12、del.addAttribute(account,account);39. 40. /跳转到用户信息页面41. returnaccount/profile;42. 43. RequestMapping(value = /profile.do)为该控制器绑定url(/profile.do)RequestMapping(method = RequestMethod.GET)指定为GET请求model.addAttribute(account, account);绑定账户return account/profile;跳转到“/WEB-INF/page/account/porfile.jsp”页面对应

13、构建这个页面:porfile.jspJsp代码1. 用户信息2. 3. 用户名:4. 5. 账户信息已经绑定在response的属性上。自然,使用标签就可以获得账户信息内容。访问地址http:/localhost:8080/spring/profile.do?id=1,结果如图所示:接着构建一个登录控制器LoginControllerLoginController.javaJava代码1. /*2. *2010-1-253. */4. packageorg.zlex.spring.controller;5. 6. importorg.springframework.beans.factory.

14、annotation.Autowired;7. importorg.springframework.stereotype.Controller;8. importorg.springframework.ui.ModelMap;9. importorg.springframework.web.bind.annotation.ModelAttribute;10. importorg.springframework.web.bind.annotation.RequestMapping;11. importorg.springframework.web.bind.annotation.RequestM

15、ethod;12. importorg.zlex.spring.domain.Account;13. importorg.zlex.spring.service.AccountService;14. 15. /*16. *登录控制器17. *18. *author梁栋19. *version1.020. *since1.021. */22. Controller23. RequestMapping(value=/login.do)24. publicclassLoginController25. 26. Autowired27. privateAccountServiceaccountServ

16、ice;28. 29. /*30. *初始化表单31. *32. *parammodel33. *return34. */35. RequestMapping(method=RequestMethod.GET)36. publicStringinitForm(ModelMapmodel)37. Accountaccount=newAccount();38. model.addAttribute(account,account);39. /直接跳转到登录页面40. returnaccount/login;41. 42. 43. /*44. *登录45. *46. *paramaccount47.

17、 *return48. */49. RequestMapping(method=RequestMethod.POST)50. publicStringlogin(ModelAttribute(account)Accountaccount)51. Accountacc=accountService.read(account.getUsername(),account52. .getPassword();53. if(acc!=null)54. returnredirect:profile.do?id=+acc.getId();55. else56. returnredirect:login.do

18、;57. 58. 59. 分段详述,先说初始化表单:Java代码1. /*2. *初始化表单3. *4. *parammodel5. *return6. */7. RequestMapping(method=RequestMethod.GET)8. publicStringinitForm(ModelMapmodel)9. Accountaccount=newAccount();10. model.addAttribute(account,account);11. /直接跳转到登录页面12. returnaccount/login;13. RequestMapping(method = Req

19、uestMethod.GET)指定了GET请求方式,这与POST表单提交相对应!model.addAttribute(account, account);绑定账户对象,也就是这个登录表单对象return account/login;指向登录页面再看登录方法:Java代码1. /*2. *登录3. *4. *paramaccount5. *return6. */7. RequestMapping(method=RequestMethod.POST)8. publicStringlogin(ModelAttribute(account)Accountaccount)9. Accountacc=ac

20、countService.read(account.getUsername(),account10. .getPassword();11. if(acc!=null)12. returnredirect:profile.do?id=+acc.getId();13. else14. returnredirect:login.do;15. 16. RequestMapping(method = RequestMethod.POST)绑定POST表单提交请求ModelAttribute(account) Account account绑定表单对象。最后,再来看看页面:login.jspJsp代码1. 登录2. 3. 4. 用户名:6. 密码:8. 9. 登录10. 重置11. 12. 13. 注意,必须指明commandName,且与表单初始化、提交方法中的表单对象名称保持一致!页面目录结构如下图所示:在页面中,我加入了一部分css效果,这部分代码我就不在这里唠叨了,大家可以看源码!登录试试,如图:用户名:snwolf 密码:zlex如果登录成功,我们就会跳转到之前的账户信息页面!注解的确减少了代码的开发量,当然,这对于我们理解程序是一种挑战!如果你不知道原有的SpringMVC的流程,很难一开始就能摆弄清楚这些内容!完整代码见附件!

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

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