Spring(三) 表单页面处理
如果要说表单,最简单的就是用户登录页面了!估计大多数做B/S出身的兄弟可能写的第一个表单就是登录表单了!
今天,我也不例外,做一个登录验证实现!
首先,改造一下账户类Account,增加一个id字段:
Account.java
/**
* 2010-1-23
*/
package org.zlex.spring.domain;
import java.io.Serializable;
/**
* 账户
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @version 1.0
* @since 1.0
*/
public class Account implements Serializable {
/**
*
*/
private static final long serialVersionUID = -533698031946372178L;
/**
* 主键
*/
private int id;
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
public Account() {
}
/**
* @param id
*/
public Account(int id) {
this.id = id;
}
// get、set方法省略
}
接下来,为了协调逻辑处理,我们改造接口AccountService及其实现类AccountServiceImpl:
AccountService.java
/**
* 2010-1-23
*/
package org.zlex.spring.service;
import org.springframework.transaction.annotation.Transactional;
import org.zlex.spring.domain.Account;
/**
* 账户业务接口
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @version 1.0
* @since 1.0
*/
@Transactional
public interface AccountService {
/**
* 获得账户
*
* @param username
* @param password
* @return
*/
Account read(String username, String password);
/**
* 获得账户
*
* @param id
* @return
*/
Account read(int id);
}
我们暂时抛开AccountDao该做的事情,在AccountServiceImpl中完成数据提取:
AccountServiceImpl.java
/**
* 2010-1-23
*/
package org.zlex.spring.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zlex.spring.dao.AccountDao;
import org.zlex.spring.domain.Account;
import org.zlex.spring.service.AccountService;
/**
* 账户业务
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @version 1.0
* @since 1.0
*/
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Override
public Account read(String username, String password) {
Account account = null;
if (username.equals("snowolf") && password.equals("zlex")) {
account = new Account();
account.setId(1);
account.setUsername(username);
account.setPassword(password);
}
return account;
}
@Override
public Account read(int id) {
Account account = new Account();
account.setId(1);
account.setUsername("snowolf");
account.setPassword("zlex");
return account;
}
}
先来一个账户信息的展示,构建一个控制器ProfileController:
ProfileController.java
/**
* 2010-1-26
*/
package org.zlex.spring.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.zlex.spring.domain.Account;
import org.zlex.spring.service.AccountService;
/**
* 账户信息控制器
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @version 1.0
* @since 1.0
*/
@Controller
@RequestMapping(value = "/profile.do")
public class ProfileController {
@Autowired
private AccountService accountService;
/**
* 账户信息展示
*
* @param id
* @param model
* @return
*/
@RequestMapping(method = RequestMethod.GET)
public String profile(@RequestParam("id") int id, ModelMap model) {
Account account = 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
Jsp代码 收藏代码
<fieldset><legend>用户信息</legend>
<ul>
<li><label>用户名:</label><c:out value="${account.username}" /></li>
</ul>
</fieldset>
账户信息已经绑定在response的属性上。自然,使用<c:out />标签就可以获得账户信息内容。
访问地址http://localhost:8080/spring/profile.do?id=1,结果如图所示:
接着构建一个登录控制器LoginController
LoginController.java
/**
* 2010-1-25
*/
package org.zlex.spring.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.zlex.spring.domain.Account;
import org.zlex.spring.service.AccountService;
/**
* 登录控制器
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @version 1.0
* @since 1.0
*/
@Controller
@RequestMapping(value = "/login.do")
public class LoginController {
@Autowired
private AccountService accountService;
/**
* 初始化表单
*
* @param model
* @return
*/
@RequestMapping(method = RequestMethod.GET)
public String initForm(ModelMap model) {
Account account = new Account();
model.addAttribute("account", account);
// 直接跳转到登录页面
return "account/login";
}
/**
* 登录
*
* @param account
* @return
*/
@RequestMapping(method = RequestMethod.POST)
public String login(@ModelAttribute("account") Account account) {
Account acc = accountService.read(account.getUsername(), account
.getPassword());
if (acc != null) {
return "redirect:profile.do?id=" + acc.getId();
} else {
return "redirect:login.do";
}
}
}
分段详述,先说初始化表单:
/**
* 初始化表单
*
* @param model
* @return
*/
@RequestMapping(method = RequestMethod.GET)
public String initForm(ModelMap model) {
Account account = new Account();
model.addAttribute("account", account);
// 直接跳转到登录页面
return "account/login";
}
@RequestMapping(method = RequestMethod.GET)指定了GET请求方式,这与POST表单提交相对应!
model.addAttribute("account", account);绑定账户对象,也就是这个登录表单对象
return "account/login";指向登录页面
再看登录方法:
/**
* 登录
*
* @param account
* @return
*/
@RequestMapping(method = RequestMethod.POST)
public String login(@ModelAttribute("account") Account account) {
Account acc = 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
Jsp代码 收藏代码
<fieldset><legend>登录</legend><form:form commandName="account">
<form:hidden path="id" />
<ul>
<li><form:label path="username">用户名:</form:label><form:input
path="username" /></li>
<li><form:label path="password">密码:</form:label><form:password
path="password" /></li>
<li>
<button type="submit">登录</button>
<button type="reset">重置</button>
</li>
</ul>
</form:form></fieldset>
注意,<form:form commandName="account">必须指明commandName,且与表单初始化、提交方法中的表单对象名称保持一致!
页面目录结构如下图所示:
用户名:snwolf 密码:zlex
如果登录成功,我们就会跳转到之前的账户信息页面!
注解的确减少了代码的开发量,当然,这对于我们理解程序是一种挑战!
如果你不知道原有的SpringMVC的流程,很难一开始就能摆弄清楚这些内容!
Spring(三) 表单页面处理相关推荐
- Spring Security默认的用户登录表单 页面源代码
Spring Security默认的用户登录表单 页面源代码 <html><head><title>Login Page</title></hea ...
- Spring进行表单验证
转自:https://www.tianmaying.com/tutorial/spring-form-validation 开发环境 IDE+Java环境(JDK 1.7或以上版本) Maven 3. ...
- C# Winform 通过FlowLayoutPanel及自定义的编辑控件,实现快速构建C/S版的编辑表单页面...
个人理解,开发应用程序的目的,不论是B/S或是C/S结构类型,无非就是实现可供用户进行查.增.改.删,其中查询用到最多,开发设计的场景也最为复杂,包括但不限于:表格记录查询.报表查询.导出文件查询等等 ...
- WSS页面定制系列(2)---定制单个列表的表单页面
这个主题的曾经写过一篇:WSS3.0开发--页面定制(1)--修改列表的表单页面 这篇文章讲诉了如何通过修改RenderingTemplate来定制整个Farm的表单页面,本篇将讲诉如何定制单个的表单 ...
- Spring MVC表单防重复提交
转载自 Spring MVC表单防重复提交 利用Spring MVC的过滤器及token传递验证来实现表单防重复提交. 创建注解 @Target(ElementType.METHOD) @Retent ...
- Spring MVC表单教程
本教程将展示如何在Spring MVC中处理表单提交. 我们将定义一个控制器来处理页面加载和表单提交. 您可以在GitHub上获取代码. 先决条件: 您应该有一个运行中的Spring MVC应用程序. ...
- Spring MVC表单验证(带批注)
这篇文章提供了一个简单HTML表单验证示例. 它基于带有注释的Spring MVC示例. 该代码可在GitHub的Spring-MVC-Form-Validation目录中找到. 数据 在此示例中,我 ...
- 微信小程序开发之——调查问卷-编写表单页面(2.2.2)
一 概述 本节介绍表单页面相关的内容: 编写调查问卷表单页面 编写调查问卷样式 设置导航栏标题 二 编写调查问卷表单页面 在微信开发者工具中创建一个新项目,然后在项目中创建pages/index/in ...
- 一个html表单页面
1.html表单的提交: <form action="" method= ""> </form> action属性设置提交的服务器地址 ...
- 如何在MVCsheet表单页面的后台取到页面自定义字段的值?
问题: <asp:HiddenField id="test" runat="server"/> 在前台加了这样一个隐藏元素,mvcsheet表单页面 ...
最新文章
- 07 分支管理 —— Feature分支
- 网速不给力,我们自己给——MinGW的手动安装与配置
- Android中BroadcastReceiver组件详解
- 【Python】while循环实现用户登录的三次机会
- linux中updatedb命令详解,updatedb命令
- Calico数据路径:IP路由和iptables
- adobe FMS(flash media server)错误解决小结
- Permission denied (publickey).
- Keil(MDK-ARM-STM32)系列教程(四)工程目标选项配置(Ⅱ)
- zookeeper之系列五:简单操作
- python 将数据写入excel
- 里氏替换原则_春辉带你了解面相对象设计第二原则(里氏替换原则)
- Codeforces 448 D. Multiplication Table
- 土木工程--钢筋下料软件(续)
- 基于H5的实时语音聊天
- mac配置VMware Fusion虚拟机网络配置
- php 微信卡券源码,微信卡券投放——货架方式
- java jar 最大内存大小_Java运行Jar包内存配置的操作
- Discuz!门户列表页,文章中无图片时,随机选择一张作为封面
- 苏黎世大学计算机研究生费用,苏黎世联邦理工学院学费需要多少