新手,刚接触spring系列,想在springside4-4.2.1GA的示例showcase中增加注册功能,一直不成功,大家帮看看!

sunwan 发布于 2014/06/07 09:34
阅读 1K+
收藏 0

新手,刚接触spring系列,想在springside4-4.2.1GA的示例showcase中增加注册功能,一直不成功,大家帮看看!

已经在AccountService中增加了registerUser方法;

在web中添加控制器 RegisterController.java:


/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package org.springside.examples.showcase.web;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.examples.showcase.entity.Role;
import org.springside.examples.showcase.entity.User;
import org.springside.examples.showcase.service.AccountService;

import com.google.common.collect.Maps;

/**
 * 用户注册的Controller.
 * 
 * @author calvin
 */
@Controller
@RequestMapping(value = "/account/register")
public class RegisterController {

	private static Map<String, String> allStatus = Maps.newHashMap();

	static {
		allStatus.put("enabled", "有效");
		allStatus.put("disabled", "无效");
	}

	@Autowired
	private AccountService accountService;

	@RequestMapping(method = RequestMethod.GET)
	public String registerForm(Model model) {
		model.addAttribute("user", new User());
		model.addAttribute("allStatus", allStatus);
		model.addAttribute("allRoles", accountService.getAllRole());
		return "account/registerForm";
	}

	// @RequiresPermissions("user:create")
	@RequestMapping(method = RequestMethod.POST)
	public String register(@Valid @ModelAttribute("user") User user,
			@RequestParam(value = "roleList") List<Long> checkedRoleList, RedirectAttributes redirectAttributes) {

		// bind roleList
		user.getRoleList().clear();
		for (Long roleId : checkedRoleList) {
			Role role = new Role(roleId);
			user.getRoleList().add(role);
		}

		accountService.registerUser(user);

		redirectAttributes.addFlashAttribute("message", "用户注册成功");
		return "redirect:/account/userList";
	}

	/**
	 * Ajax请求校验loginName是否唯一。
	 */
	@RequestMapping(value = "checkLoginName")
	@ResponseBody
	public String checkLoginName(@RequestParam("loginName") String loginName) {
		if (accountService.findUserByLoginName(loginName) == null) {
			return "true";
		} else {
			return "false";
		}
	}
}
在views/account中添加 registerForm.jsp(修改自userForm.jsp):



<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springside.org.cn/tags/form" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<html>
<head>
	<title>用户注册</title>
	<script src="${ctx}/static/jquery-validation/1.11.1/jquery.validate.min.js" type="text/javascript"></script>
	<script src="${ctx}/static/jquery-validation/1.11.1/messages_bs_zh.js" type="text/javascript"></script>
	<link href="${ctx}/static/jquery-validation/1.11.1/validate.css" type="text/css" rel="stylesheet" />
	<script>
		$(document).ready(function() {
			$("#account-tab").addClass("active");
			//聚焦第一个输入框
			$("#loginName").focus();
			//为inputForm注册validate函数
			$("#inputForm").validate({
				rules: {
					loginName: {
						remote: "${ctx}/account/register/checkLoginName"
					},
					roleList:"required"
				},
				messages: {
					loginName: {
						remote: "用户登录名已存在"
					}
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					if ( element.is(":checkbox") )
						error.appendTo(element.parent().next());
					else
						error.insertAfter(element);
				}
			});
		});
	</script>
</head>

<body>
	<h1>用户注册</h1>
	<form:form id="inputForm" modelAttribute="user" action="${ctx}/account/register" method="post" class="form-horizontal">
		<fieldset>
			<legend><small>用户注册</small></legend>
			<div id="messageBox" class="alert alert-error input-large controls" style="display:none">输入有误,请先更正。</div>
			<div class="control-group">
				<label for="loginName" class="control-label">登录名:</label>
				<div class="controls">
					<input type="text" id="loginName" name="loginName" class="input-large required"/>
				</div>
			</div>
			<div class="control-group">
				<label for="name" class="control-label">用户名:</label>
				<div class="controls">
					<input type="text" id="name" name="name" class="input-large required"/>
				</div>
			</div>
			<div class="control-group">
				<label for="plainPassword" class="control-label">密码:</label>
				<div class="controls">
					<input type="password" id="plainPassword" name="plainPassword" class="input-large required" placeholder="请输入密码"/>
				</div>
			</div>
			<div class="control-group">
				<label for="confirmPassword" class="control-label">确认密码:</label>
				<div class="controls">
					<input type="password" id="confirmPassword" name="confirmPassword" class="input-large required" equalTo="#plainPassword"/>
				</div>
			</div>
			<div class="control-group">
				<label for="groupList" class="control-label">角色:</label>
				<div class="controls">
					<form:bscheckboxes path="roleList" items="${allRoles}" itemLabel="name" itemValue="id" />
				</div>
			</div>	
			<div class="control-group">
				<label for="status" class="control-label">状态:</label>
				<div class="controls">
					<form:bsradiobuttons path="status" items="${allStatus}" labelCssClass="inline"/>
				</div>
			</div>
			<div class="form-actions">
				<input id="submit_btn" class="btn btn-primary" type="submit" value="提交"/>&nbsp;	
				<input id="cancel_btn" class="btn" type="button" value="返回" onclick="history.back()"/>
				<p class="help-block">(保存后将发送JMS消息通知改动,而消息接收者将发送提醒邮件)</p>			
			</div>
		</fieldset>
	</form:form>
</body>
</html>
调试运行,以admin登录,进入增加用户(注册)页面,填写资料,一切正常,但提交后就不正常了,在firefox下,提交后一片空白,查看源代码也是空的;在IE下出现400错误。


在控制器的register中打上断点,没有执行到,但这段代码的 @Valid 注解好像又是起作用的,这段代码到底有没有运行到?


// @RequiresPermissions("user:create")
	@RequestMapping(method = RequestMethod.POST)
	public String register(@Valid @ModelAttribute("user") User user,
			@RequestParam(value = "roleList") List<Long> checkedRoleList, RedirectAttributes redirectAttributes) {

		// bind roleList
             //下面这行打了断点,没运行到。
             user.getRoleList().clear();
		for (Long roleId : checkedRoleList) {
			Role role = new Role(roleId);
			user.getRoleList().add(role);
		}

		accountService.registerUser(user);

		redirectAttributes.addFlashAttribute("message", "用户注册成功");
		return "redirect:/account/userList";
	}



控制台没有提示错误,后来也简化了注册页面,也改变了提交路径,也还是同样的现象,网上搜了大片,搞了2天了,也没搞定,希望各位看是什么错误!


加载中
0
s
sunwan

自己搞定了,原来要先解除rolelist的绑定!

在RegisterController中添加:

/**
	 * 不自动绑定对象中的roleList属性,另行处理。
	 */
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.setDisallowedFields("roleList");
	}



返回顶部
顶部