SpringSide是个什么东西呢?这么说吧,就是“采众家之长”的一个一站式框架,它吸取了开源界许多优秀组件的精华部分,非常简约的一个东西,具体就不多介绍了,自己可以参考官方文档。
下面来看看运用这个框架实现一个简单的用户管理究竟有多么容易。
先来看表现层:
新增或修改用户页面:
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/commons/taglibs.jsp" %>
<html>
<head>
<%@ include file="/commons/meta.jsp" %>
<title>User Manage</title>
</head>
<body>
<div id="page">
<div id="header">
<h1>Helloworld Sample</h1>
</div>
<%@ include file="/commons/messages.jsp" %>
<div id="content">
<h1>User Infomation Manage</h1>
<html:form action="/user.do" focus="name" styleClass="form" onsubmit="return validateUserForm(this)">
<input type="hidden" name="method" value="save"/>
<html:hidden property="id"/>
<table>
<tr>
<td><label>Name</label></td>
<td>
<html:text property="name" styleClass="text"/>
<span class="req">*</span>
<span class="fieldError"><html:errors property="name"/></span>
</td>
</tr>
<tr>
<td><label>EMail</label></td>
<td>
<html:text property="email" styleClass="text"/>
</td>
</tr>
<tr>
<td><label>Remark</label></td>
<td>
<html:textarea property="descn" rows="10" cols="40"/>
</td>
</tr>
</table>
<div>
<html:submit property="saveBtn" styleClass="button">Save</html:submit>
<html:cancel styleClass="button">Cancel</html:cancel>
</div>
</html:form>
</div>
</div>
<html:javascript formName="userForm" staticJavascript="false" dynamicJavascript="true" cdata="false"/>
<script type="text/javascript" src="${ctx}/scripts/validator.jsp"></script>
<%@ include file="/commons/footer.jsp" %>
</body>
</html>
<%@ include file="/commons/taglibs.jsp" %>
<html>
<head>
<%@ include file="/commons/meta.jsp" %>
<title>User Manage</title>
</head>
<body>
<div id="page">
<div id="header">
<h1>Helloworld Sample</h1>
</div>
<%@ include file="/commons/messages.jsp" %>
<div id="content">
<h1>User Infomation Manage</h1>
<html:form action="/user.do" focus="name" styleClass="form" onsubmit="return validateUserForm(this)">
<input type="hidden" name="method" value="save"/>
<html:hidden property="id"/>
<table>
<tr>
<td><label>Name</label></td>
<td>
<html:text property="name" styleClass="text"/>
<span class="req">*</span>
<span class="fieldError"><html:errors property="name"/></span>
</td>
</tr>
<tr>
<td><label>EMail</label></td>
<td>
<html:text property="email" styleClass="text"/>
</td>
</tr>
<tr>
<td><label>Remark</label></td>
<td>
<html:textarea property="descn" rows="10" cols="40"/>
</td>
</tr>
</table>
<div>
<html:submit property="saveBtn" styleClass="button">Save</html:submit>
<html:cancel styleClass="button">Cancel</html:cancel>
</div>
</html:form>
</div>
</div>
<html:javascript formName="userForm" staticJavascript="false" dynamicJavascript="true" cdata="false"/>
<script type="text/javascript" src="${ctx}/scripts/validator.jsp"></script>
<%@ include file="/commons/footer.jsp" %>
</body>
</html>
用户列表页面:
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/commons/taglibs.jsp" %>
<html>
<head>
<%@ include file="/commons/meta.jsp" %>
<link href="${ctx}/widgets/extremecomponents/extremecomponents.css" type="text/css" rel="stylesheet">
<title>User Manage</title>
</head>
<body>
<div id="page">
<div id="header">
<h1>Helloworld Sample</h1>
</div>
<div id="content">
<h1>User List</h1>
<%@ include file="/commons/messages.jsp" %>
<ec:table items="users" var="user"
action="${ctx}/user.do">
<ec:exportXls fileName="UserList.xls" tooltip="Export Excel"/>
<ec:row>
<ec:column property="rowcount" cell="rowCount" sortable="false" title="No." width="60"/>
<ec:column property="id" title="ID" width="60"/>
<ec:column property="name" title="Name" width="120"/>
<ec:column property="email" title="Email" width="120"/>
<ec:column property="descn" title="Description" viewsDenied="html"/>
<ec:column property="null" title="Edit" width="40" sortable="false" viewsAllowed="html">
<a href="user.do?method=edit&id=${user.id}">Edit</a>
</ec:column>
<ec:column property="null" title="Remove" width="40" sortable="false" viewsAllowed="html">
<a href="user.do?method=delete&id=${user.id}">Delete</a>
</ec:column>
</ec:row>
</ec:table>
</div>
<div>
<button id="addbtn" onclick="location.href='user.do?method=create'">Add</button>
</div>
</div>
<%@ include file="/commons/footer.jsp" %>
</body>
</html>
<%@ include file="/commons/taglibs.jsp" %>
<html>
<head>
<%@ include file="/commons/meta.jsp" %>
<link href="${ctx}/widgets/extremecomponents/extremecomponents.css" type="text/css" rel="stylesheet">
<title>User Manage</title>
</head>
<body>
<div id="page">
<div id="header">
<h1>Helloworld Sample</h1>
</div>
<div id="content">
<h1>User List</h1>
<%@ include file="/commons/messages.jsp" %>
<ec:table items="users" var="user"
action="${ctx}/user.do">
<ec:exportXls fileName="UserList.xls" tooltip="Export Excel"/>
<ec:row>
<ec:column property="rowcount" cell="rowCount" sortable="false" title="No." width="60"/>
<ec:column property="id" title="ID" width="60"/>
<ec:column property="name" title="Name" width="120"/>
<ec:column property="email" title="Email" width="120"/>
<ec:column property="descn" title="Description" viewsDenied="html"/>
<ec:column property="null" title="Edit" width="40" sortable="false" viewsAllowed="html">
<a href="user.do?method=edit&id=${user.id}">Edit</a>
</ec:column>
<ec:column property="null" title="Remove" width="40" sortable="false" viewsAllowed="html">
<a href="user.do?method=delete&id=${user.id}">Delete</a>
</ec:column>
</ec:row>
</ec:table>
</div>
<div>
<button id="addbtn" onclick="location.href='user.do?method=create'">Add</button>
</div>
</div>
<%@ include file="/commons/footer.jsp" %>
</body>
</html>
对应的控制器类UserAction.java:
package org.springside.helloworld.web;
import org.springside.core.web.StrutsEntityAction;
import org.springside.helloworld.model.User;
import org.springside.helloworld.service.UserManager;
/** *//**
* 用户管理Controller.
* <p/>
* 继承于StrutsEntityAction,不需编码就拥有默认的对User对象的CRUD响应函数. 如果想了解不继承于EntityAction,自行编写CRUD的写法, 参考{@link UserActionNativeVersion}.
*
* @author calvin
* @see org.springside.core.web.StrutsEntityAction
* @see org.springside.core.web.StrutsAction
* @see UserActionNativeVersion
*/
public class UserAction extends StrutsEntityAction<User, UserManager> {
@SuppressWarnings("unused")
private UserManager userManager;
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
}
import org.springside.core.web.StrutsEntityAction;
import org.springside.helloworld.model.User;
import org.springside.helloworld.service.UserManager;
/** *//**
* 用户管理Controller.
* <p/>
* 继承于StrutsEntityAction,不需编码就拥有默认的对User对象的CRUD响应函数. 如果想了解不继承于EntityAction,自行编写CRUD的写法, 参考{@link UserActionNativeVersion}.
*
* @author calvin
* @see org.springside.core.web.StrutsEntityAction
* @see org.springside.core.web.StrutsAction
* @see UserActionNativeVersion
*/
public class UserAction extends StrutsEntityAction<User, UserManager> {
@SuppressWarnings("unused")
private UserManager userManager;
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
}
然后是业务逻辑层,
package org.springside.helloworld.service;
import org.springside.core.dao.HibernateEntityDao;
import org.springside.helloworld.model.User;
/** *//**
* 用户管理业务类.
* <p/>
* 继承于HibernateEntityDao,不需任何代码即拥有默认的对User对象的CRUD函数. 如果想了解不继承于EntityDao,自行编写CRUD的写法, 参考{@link UserManagerNativeVersion}.
*
* @author calvin
* @see HibernateEntityDao
* @see org.springside.core.dao.HibernateGenericDao
* @see UserManagerNativeVersion
*/
public class UserManager extends HibernateEntityDao<User> {
// .CRUD以外的其它商业方法
}
import org.springside.core.dao.HibernateEntityDao;
import org.springside.helloworld.model.User;
/** *//**
* 用户管理业务类.
* <p/>
* 继承于HibernateEntityDao,不需任何代码即拥有默认的对User对象的CRUD函数. 如果想了解不继承于EntityDao,自行编写CRUD的写法, 参考{@link UserManagerNativeVersion}.
*
* @author calvin
* @see HibernateEntityDao
* @see org.springside.core.dao.HibernateGenericDao
* @see UserManagerNativeVersion
*/
public class UserManager extends HibernateEntityDao<User> {
// .CRUD以外的其它商业方法
}
然后是模型层
package org.springside.helloworld.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/** *//**
* 用户.带jpa annotation简版配置.
*
* @author calvin
* @author schweigen
*/
//同USERS表映射
@Entity
@Table(name = "USERS")
public class User
{
private Integer id;//用户id
private String name;//用户名
private String email;//e-mail
private String descn;//自我介绍
//主键自动生成,其他,其余属性全部与数据库中的列默认映射。
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDescn() {
return descn;
}
public void setDescn(String descn) {
this.descn = descn;
}
}
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/** *//**
* 用户.带jpa annotation简版配置.
*
* @author calvin
* @author schweigen
*/
//同USERS表映射
@Entity
@Table(name = "USERS")
public class User
{
private Integer id;//用户id
private String name;//用户名
private String email;//e-mail
private String descn;//自我介绍
//主键自动生成,其他,其余属性全部与数据库中的列默认映射。
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDescn() {
return descn;
}
public void setDescn(String descn) {
this.descn = descn;
}
}
那么代码部分就这些了,可以看到不需要我们自己去写重复的CRUD代码,仅仅从一些特定的基类继承下来就可以了,而Jdk新加入的泛型技术的运用更是如虎添翼。那么对于配置文件部分,我个人感觉比以前好像更加复杂了呢,也许是还不习惯吧。。。