#85 spring mvc taglib的checkbox, radio 支持twitter bootstrap

This commit is contained in:
Calvin
2012-09-01 01:25:05 +08:00
parent 7cdbe51d1a
commit a5446be3bb
11 changed files with 5384 additions and 26 deletions
@@ -64,15 +64,20 @@ public class ShiroDbRealm extends AuthorizingRealm {
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
ShiroUser shiroUser = (ShiroUser) principals.fromRealm(getName()).iterator().next();
User user = accountService.findUserByLoginName(shiroUser.loginName);
if (user != null) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.addRoles(user.getRoleList());
return info;
} else {
Object principal = principals.getPrimaryPrincipal();
if (principal == null) {
return null;
}
ShiroUser shiroUser = (ShiroUser) principal;
User user = accountService.findUserByLoginName(shiroUser.loginName);
if (user == null) {
return null;
}
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.addRoles(user.getRoleList());
return info;
}
/**
@@ -13,7 +13,8 @@ public class QuickStartServer {
public static final int PORT = 8080;
public static final String CONTEXT = "/quickstart";
public static final String BASE_URL = "http://localhost:8080/quickstart";
public static final String[] TLD_JAR_NAMES = new String[] { "sitemesh", "spring-webmvc", "shiro-web" };
public static final String[] TLD_JAR_NAMES = new String[] { "sitemesh", "spring-webmvc", "shiro-web",
"springside-core" };
public static void main(String[] args) throws Exception {
//设定Spring的profile
@@ -70,20 +70,25 @@ public class ShiroDbRealm extends AuthorizingRealm {
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
ShiroUser shiroUser = (ShiroUser) principals.fromRealm(getName()).iterator().next();
User user = accountService.findUserByLoginName(shiroUser.loginName);
if (user != null) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
for (Role role : user.getRoleList()) {
//基于Role的权限信息
info.addRole(role.getName());
//基于Permission的权限信息
info.addStringPermissions(role.getPermissionList());
}
return info;
} else {
Object principal = principals.getPrimaryPrincipal();
if (principal == null) {
return null;
}
ShiroUser shiroUser = (ShiroUser) principal;
User user = accountService.findUserByLoginName(shiroUser.loginName);
if (user == null) {
return null;
}
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
for (Role role : user.getRoleList()) {
//基于Role的权限信息
info.addRole(role.getName());
//基于Permission的权限信息
info.addStringPermissions(role.getPermissionList());
}
return info;
}
/**
@@ -1,6 +1,7 @@
package org.springside.examples.showcase.web;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
@@ -19,7 +20,7 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.examples.showcase.entity.User;
import org.springside.examples.showcase.service.AccountService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
@Controller
@RequestMapping(value = "/account/user")
@@ -28,9 +29,16 @@ public class UserController {
@Autowired
private AccountService accountService;
private static Map<String, String> allStatus = Maps.newHashMap();
static {
allStatus.put("enabled", "Enabled");
allStatus.put("disabled", "Disabled");
}
//特别设定多个ReuireRoles之间为Or关系,而不是默认的And.
@RequiresRoles(value = { "Admin", "User" }, logical = Logical.OR)
@RequestMapping(value = { "list", "" })
@RequestMapping(value = "")
public String list(Model model) {
List<User> users = accountService.getAllUser();
model.addAttribute("users", users);
@@ -41,7 +49,6 @@ public class UserController {
@RequestMapping(value = "update/{id}")
public String updateForm(@PathVariable("id") Long id, Model model) {
model.addAttribute("user", accountService.getUser(id));
List<String> allStatus = Lists.newArrayList("enabled", "disabled");
model.addAttribute("allStatus", allStatus);
return "account/userForm";
}
File diff suppressed because it is too large Load Diff
@@ -1,6 +1,6 @@
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ 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>
@@ -59,7 +59,7 @@
<div class="control-group">
<label for="status" class="control-label">状态:</label>
<div class="controls">
<form:select path="status" items="${allStatus}"/>
<form:bsradiobuttons path="status" items="${allStatus}" />
</div>
</div>
<div class="form-actions">
+9
View File
@@ -43,6 +43,15 @@
</dependency>
<!-- Security end -->
<!-- Web begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<optional>true</optional>
</dependency>
<!-- Wed end -->
<!-- UTILS begin -->
<dependency>
<groupId>com.google.guava</groupId>
@@ -0,0 +1,125 @@
package org.springside.modules.web.taglib;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.jsp.JspException;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.tags.form.AbstractMultiCheckedElementTag;
import org.springframework.web.servlet.tags.form.TagWriter;
public abstract class BSAbstractMultiCheckedElementTag extends AbstractMultiCheckedElementTag {
/**
* Renders the '<code>input type="radio"</code>' element with the configured
* {@link #setItems(Object)} values. Marks the element as checked if the
* value matches the bound value.
*/
@Override
protected int writeTagContent(TagWriter tagWriter) throws JspException {
Object items = getItems();
Object itemsObject = (items instanceof String ? evaluate("items", items) : items);
String itemValue = getItemValue();
String itemLabel = getItemLabel();
String valueProperty = (itemValue != null ? ObjectUtils.getDisplayString(evaluate("itemValue", itemValue))
: null);
String labelProperty = (itemLabel != null ? ObjectUtils.getDisplayString(evaluate("itemLabel", itemLabel))
: null);
Class<?> boundType = getBindStatus().getValueType();
if (itemsObject == null && boundType != null && boundType.isEnum()) {
itemsObject = boundType.getEnumConstants();
}
if (itemsObject == null) {
throw new IllegalArgumentException(
"Attribute 'items' is required and must be a Collection, an Array or a Map");
}
if (itemsObject.getClass().isArray()) {
Object[] itemsArray = (Object[]) itemsObject;
for (int i = 0; i < itemsArray.length; i++) {
Object item = itemsArray[i];
writeObjectEntry(tagWriter, valueProperty, labelProperty, item, i);
}
} else if (itemsObject instanceof Collection) {
final Collection optionCollection = (Collection) itemsObject;
int itemIndex = 0;
for (Iterator it = optionCollection.iterator(); it.hasNext(); itemIndex++) {
Object item = it.next();
writeObjectEntry(tagWriter, valueProperty, labelProperty, item, itemIndex);
}
} else if (itemsObject instanceof Map) {
final Map optionMap = (Map) itemsObject;
int itemIndex = 0;
for (Iterator it = optionMap.entrySet().iterator(); it.hasNext(); itemIndex++) {
Map.Entry entry = (Map.Entry) it.next();
writeMapEntry(tagWriter, valueProperty, labelProperty, entry, itemIndex);
}
} else {
throw new IllegalArgumentException("Attribute 'items' must be an array, a Collection or a Map");
}
return SKIP_BODY;
}
private void writeObjectEntry(TagWriter tagWriter, String valueProperty, String labelProperty, Object item,
int itemIndex) throws JspException {
BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(item);
Object renderValue;
if (valueProperty != null) {
renderValue = wrapper.getPropertyValue(valueProperty);
} else if (item instanceof Enum) {
renderValue = ((Enum<?>) item).name();
} else {
renderValue = item;
}
Object renderLabel = (labelProperty != null ? wrapper.getPropertyValue(labelProperty) : item);
writeElementTag(tagWriter, item, renderValue, renderLabel, itemIndex);
}
private void writeMapEntry(TagWriter tagWriter, String valueProperty, String labelProperty, Map.Entry entry,
int itemIndex) throws JspException {
Object mapKey = entry.getKey();
Object mapValue = entry.getValue();
BeanWrapper mapKeyWrapper = PropertyAccessorFactory.forBeanPropertyAccess(mapKey);
BeanWrapper mapValueWrapper = PropertyAccessorFactory.forBeanPropertyAccess(mapValue);
Object renderValue = (valueProperty != null ? mapKeyWrapper.getPropertyValue(valueProperty) : mapKey.toString());
Object renderLabel = (labelProperty != null ? mapValueWrapper.getPropertyValue(labelProperty) : mapValue
.toString());
writeElementTag(tagWriter, mapKey, renderValue, renderLabel, itemIndex);
}
private void writeElementTag(TagWriter tagWriter, Object item, Object value, Object label, int itemIndex)
throws JspException {
String id = resolveId();
tagWriter.startTag("label");
tagWriter.writeAttribute("for", id);
tagWriter.writeAttribute("class", getInputType());
if (itemIndex > 0) {
Object resolvedDelimiter = evaluate("delimiter", getDelimiter());
if (resolvedDelimiter != null) {
tagWriter.appendValue(resolvedDelimiter.toString());
}
}
tagWriter.startTag("input");
writeOptionalAttribute(tagWriter, "id", id);
writeOptionalAttribute(tagWriter, "name", getName());
writeOptionalAttributes(tagWriter);
tagWriter.writeAttribute("type", getInputType());
renderFromValue(item, value, tagWriter);
tagWriter.endTag();
tagWriter.appendValue(convertToDisplayString(label));
tagWriter.endTag();
}
}
@@ -0,0 +1,8 @@
package org.springside.modules.web.taglib;
public class BSRadioButtonsTag extends BSAbstractMultiCheckedElementTag {
@Override
protected String getInputType() {
return "radio";
}
}
File diff suppressed because it is too large Load Diff
@@ -30,7 +30,7 @@
<uri>http://www.springside.org.cn/tags/shiro</uri>
<description>Apache Shiro JSP Tag Library.</description>
<description>Apache Shiro JSP Tag Library extends the hasAnyPermissions tag.</description>
<tag>
<name>hasPermission</name>