`
- 浏览:
191366 次
- 性别:
- 来自:
上海
-
如果要说表单,最简单的就是用户登录页面了!估计大多数做B/S出身的兄弟可能写的第一个表单就是登录表单了!今天,我也不例外,做一个登录验证实现!首先,改造一下账户类Account,增加一个id字段:Account.java
-
-
-
-
packageorg.zlex.spring.domain;
-
-
importjava.io.Serializable;
-
-
-
-
-
-
-
-
-
publicclassAccountimplementsSerializable{
-
-
-
-
-
privatestaticfinallongserialVersionUID=-533698031946372178L;
-
-
-
-
-
privateintid;
-
-
-
-
privateStringusername;
-
-
-
-
privateStringpassword;
-
-
-
-
publicAccount(){
-
}
-
-
-
-
-
publicAccount(intid){
-
this.id=id;
-
}
-
-
-
-
}
接下来,为了协调逻辑处理,我们改造接口AccountService及其实现类AccountServiceImpl:AccountService.java
-
-
-
-
packageorg.zlex.spring.service;
-
-
importorg.springframework.transaction.annotation.Transactional;
-
importorg.zlex.spring.domain.Account;
-
-
-
-
-
-
-
-
-
@Transactional
-
publicinterfaceAccountService{
-
-
-
-
-
-
-
-
-
Accountread(Stringusername,Stringpassword);
-
-
-
-
-
-
-
-
Accountread(intid);
-
}
我们暂时抛开AccountDao该做的事情,在AccountServiceImpl中完成数据提取:AccountServiceImpl.java
-
-
-
-
packageorg.zlex.spring.service.impl;
-
-
importorg.springframework.beans.factory.annotation.Autowired;
-
importorg.springframework.stereotype.Service;
-
importorg.zlex.spring.dao.AccountDao;
-
importorg.zlex.spring.domain.Account;
-
importorg.zlex.spring.service.AccountService;
-
-
-
-
-
-
-
-
-
@Service
-
publicclassAccountServiceImplimplementsAccountService{
-
-
@Autowired
-
privateAccountDaoaccountDao;
-
-
@Override
-
publicAccountread(Stringusername,Stringpassword){
-
Accountaccount=null;
-
if(username.equals("snowolf")&&password.equals("zlex")){
-
account=newAccount();
-
account.setId(1);
-
account.setUsername(username);
-
account.setPassword(password);
-
}
-
returnaccount;
-
}
-
-
@Override
-
publicAccountread(intid){
-
Accountaccount=newAccount();
-
account.setId(1);
-
account.setUsername("snowolf");
-
account.setPassword("zlex");
-
returnaccount;
-
}
-
}
先来一个账户信息的展示,构建一个控制器ProfileController:ProfileController.java
-
-
-
-
packageorg.zlex.spring.controller;
-
-
importorg.springframework.beans.factory.annotation.Autowired;
-
importorg.springframework.stereotype.Controller;
-
importorg.springframework.ui.ModelMap;
-
importorg.springframework.web.bind.annotation.RequestMapping;
-
importorg.springframework.web.bind.annotation.RequestMethod;
-
importorg.springframework.web.bind.annotation.RequestParam;
-
importorg.zlex.spring.domain.Account;
-
importorg.zlex.spring.service.AccountService;
-
-
-
-
-
-
-
-
-
@Controller
-
@RequestMapping(value="/profile.do")
-
publicclassProfileController{
-
@Autowired
-
privateAccountServiceaccountService;
-
-
-
-
-
-
-
-
-
@RequestMapping(method=RequestMethod.GET)
-
publicStringprofile(@RequestParam("id")intid,ModelMapmodel){
-
Accountaccount=accountService.read(id);
-
model.addAttribute("account",account);
-
-
-
return"account/profile";
-
}
-
}
@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”页面对应构建这个页面:porfile.jsp
-
<fieldset><legend>用户信息</legend>
-
<ul>
-
<li><label>用户名:</label><c:outvalue="${account.username}"/></li>
-
</ul>
-
</fieldset>
账户信息已经绑定在response的属性上。自然,使用<c:out />标签就可以获得账户信息内容。访问地址http://localhost:8080/spring/profile.do?id=1,结果如图所示:接着构建一个登录控制器LoginControllerLoginController.java
-
-
-
-
packageorg.zlex.spring.controller;
-
-
importorg.springframework.beans.factory.annotation.Autowired;
-
importorg.springframework.stereotype.Controller;
-
importorg.springframework.ui.ModelMap;
-
importorg.springframework.web.bind.annotation.ModelAttribute;
-
importorg.springframework.web.bind.annotation.RequestMapping;
-
importorg.springframework.web.bind.annotation.RequestMethod;
-
importorg.zlex.spring.domain.Account;
-
importorg.zlex.spring.service.AccountService;
-
-
-
-
-
-
-
-
-
@Controller
-
@RequestMapping(value="/login.do")
-
publicclassLoginController{
-
-
@Autowired
-
privateAccountServiceaccountService;
-
-
-
-
-
-
-
-
@RequestMapping(method=RequestMethod.GET)
-
publicStringinitForm(ModelMapmodel){
-
Accountaccount=newAccount();
-
model.addAttribute("account",account);
-
-
return"account/login";
-
}
-
-
-
-
-
-
-
-
@RequestMapping(method=RequestMethod.POST)
-
publicStringlogin(@ModelAttribute("account")Accountaccount){
-
Accountacc=accountService.read(account.getUsername(),account
-
.getPassword());
-
if(acc!=null){
-
return"redirect:profile.do?id="+acc.getId();
-
}else{
-
return"redirect:login.do";
-
}
-
}
-
}
分段详述,先说初始化表单:
-
-
-
-
-
-
-
@RequestMapping(method=RequestMethod.GET)
-
publicStringinitForm(ModelMapmodel){
-
Accountaccount=newAccount();
-
model.addAttribute("account",account);
-
-
return"account/login";
-
}
@RequestMapping(method = RequestMethod.GET)指定了GET请求方式,这与POST表单提交相对应!model.addAttribute("account", account);绑定账户对象,也就是这个登录表单对象return "account/login";指向登录页面再看登录方法:
-
-
-
-
-
-
-
@RequestMapping(method=RequestMethod.POST)
-
publicStringlogin(@ModelAttribute("account")Accountaccount){
-
Accountacc=accountService.read(account.getUsername(),account
-
.getPassword());
-
if(acc!=null){
-
return"redirect:profile.do?id="+acc.getId();
-
}else{
-
return"redirect:login.do";
-
}
-
}
@RequestMapping(method = RequestMethod.POST)绑定POST表单提交请求@ModelAttribute("account") Account account绑定表单对象。最后,再来看看页面:login.jsp
-
<fieldset><legend>登录</legend><form:formcommandName="account">
-
<form:hiddenpath="id"/>
-
<ul>
-
<li><form:labelpath="username">用户名:</form:label><form:input
-
path="username"/></li>
-
<li><form:labelpath="password">密码:</form:label><form:password
-
path="password"/></li>
-
<li>
-
<buttontype="submit">登录</button>
-
<buttontype="reset">重置</button>
-
</li>
-
</ul>
-
</form:form></fieldset>
注意,<form:form commandName="account">必须指明commandName,且与表单初始化、提交方法中的表单对象名称保持一致!页面目录结构如下图所示:在页面中,我加入了一部分css效果,这部分代码我就不在这里唠叨了,大家可以看源码!登录试试,如图:用户名:snwolf 密码:zlex如果登录成功,我们就会跳转到之前的账户信息页面!注解的确减少了代码的开发量,当然,这对于我们理解程序是一种挑战!如果你不知道原有的SpringMVC的流程,很难一开始就能摆弄清楚这些内容!
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
Spring 注解学习手札,个人学习Spring mvc的知识总结
NULL 博文链接:https://snowolf.iteye.com/blog/577989
NULL 博文链接:https://snowolf.iteye.com/blog/583161
NULL 博文链接:https://snowolf.iteye.com/blog/587602
NULL 博文链接:https://snowolf.iteye.com/blog/578452
NULL 博文链接:https://snowolf.iteye.com/blog/588351
NULL 博文链接:https://snowolf.iteye.com/blog/1628861
分享Perl_学习手札书籍,供大家学习、参考及交流perl。
perl学习手札,共同学习,共同进步。希望你能很快掌握perl。
关于作者: 简信昌 “傲尔网”专案经理 博仲法律事务所资讯部门 台北Perl推广组 (Taipei.pm) 召集人 Newzilla召集人 目前专案: Open Source Foundry (OSSF) Newzilla 线上杂志 ...“Perl学习手札”
Perl学习手札.chmPerl学习手札.chmPerl学习手札.chm
Perl 学习手札,这本书介绍了perl的基本语法及perl的一些用法,想了解perl的就看一看
hibernate学习手札.z03
Perl 学习手札,好东西
hibernate学习手札.z01
本资料完全来源于网上收集,对于作者并没有做过多的考证,考虑到作者本身,所以保持原作品不变只是略加整理,为用户提供一个方便的浏览方式。
1-高级perl编程(黑豹书).pdf 2-Perl_学习手札.pdf
8086汇编学习手札及各章节检测题详细答案。 配套书籍 王爽的汇编
非常著名的perl学习手札(初学者有福了),本文档是精美的PDF版,排版精美,是网络上到现在为止能找到的最好的。 全书共分17个小节,通过讲解实例来学习perl,你可以复制这些代码,然后执行。学习非常方便!