学习springside3(一)
根据官网的 快速开始指南和 SpringSide 复活两篇文章,简单了解ss3的大致框架,并成功建立基于ss3风格的项目。
但其中也遇到几个小问题,首先就是Maven,由于以前安装过可能有些配置被改变了导致无法正常执行quickstart-all-in-one.bat。后来直接指定ss3中的maven目录就可以了,怀疑是maven\conf\settings.xml问题。另外一个问题就是在建立自己的项目后,执行copy-jar.bat总是不成功(有功夫要好好研究一下maven),最后只好直接把examples\mini-web\lib和examples\mini-web\webapp\WEB-INF\lib下的jar都拷过来。
建好了项目准备自己添加点功能试试,可是没有找到能够自动生成代码的脚本,只好自己一点点添加,还好ss3在配置方面简便了许多。
1、生成实体类
3、生成web相关文件
虽然最终成功使用,但是其中遇到一个非常恼火的事情。由于定义的表名是dict_nation所以后面相关的名称都是DictNation和dictNation,结果新增的功能无法实现,怀疑是违反了某个框架的命名规范,导致无法实现,有空再深入学习ss3。
但其中也遇到几个小问题,首先就是Maven,由于以前安装过可能有些配置被改变了导致无法正常执行quickstart-all-in-one.bat。后来直接指定ss3中的maven目录就可以了,怀疑是maven\conf\settings.xml问题。另外一个问题就是在建立自己的项目后,执行copy-jar.bat总是不成功(有功夫要好好研究一下maven),最后只好直接把examples\mini-web\lib和examples\mini-web\webapp\WEB-INF\lib下的jar都拷过来。
建好了项目准备自己添加点功能试试,可是没有找到能够自动生成代码的脚本,只好自己一点点添加,还好ss3在配置方面简便了许多。
1、生成实体类
@Entity
@Table(name = " dict_nation " )
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Nation extends IdEntity {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString( this );
}
}
2、生成dao、service类
@Table(name = " dict_nation " )
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Nation extends IdEntity {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString( this );
}
}
@Repository
public class NationDao extends HibernateDao < Nation, Long > {
public Nation loadByName(String name) {
return findUniqueByProperty( " name " , name);
}
}
public class NationDao extends HibernateDao < Nation, Long > {
public Nation loadByName(String name) {
return findUniqueByProperty( " name " , name);
}
}
@Service
@Transactional
public class NationManager extends EntityManager < Nation, Long > {
@Autowired
private NationDao nationDao;
@Override
public NationDao getEntityDao() {
return nationDao;
}
public Nation getNation(String name) {
return nationDao.loadByName(name);
}
}
以上操作都不用修改配置文件,真是很方便。
@Transactional
public class NationManager extends EntityManager < Nation, Long > {
@Autowired
private NationDao nationDao;
@Override
public NationDao getEntityDao() {
return nationDao;
}
public Nation getNation(String name) {
return nationDao.loadByName(name);
}
}
3、生成web相关文件
@SuppressWarnings(
"
serial
"
)
@Results( { @Result(name = CRUDActionSupport.RELOAD, location = " nation.action " , type = " redirect " ) })
public class NationAction extends CRUDActionSupport < Nation > {
@Autowired
private NationManager nationManager;
// 基本属性
private Nation entity;
private Long id;
private List < Nation > allNations;
public Nation getModel() {
return entity;
}
@Override
protected void prepareModel() throws Exception {
if (id != null ) {
entity = nationManager.get(id);
} else {
entity = new Nation();
}
}
public void setId(Long id) {
this .id = id;
}
public List < Nation > getAllNations() {
System.out.println( " test " );
return allNations;
}
@Override
public String delete() throws Exception {
try {
nationManager.delete(id);
addActionMessage( " 删除角色成功 " );
} catch (ServiceException e) {
logger.error(e.getMessage(), e);
addActionMessage(e.getMessage());
}
return RELOAD;
}
@Override
public String list() throws Exception {
allNations = nationManager.getAll();
return SUCCESS;
}
@Override
public String input() throws Exception {
System.out.println( " test " );
return INPUT;
}
@Override
public String save() throws Exception {
nationManager.save(entity);
addActionMessage( " 保存用户成功 " );
return RELOAD;
}
}
修改安全文件applicationContext-security.xml
@Results( { @Result(name = CRUDActionSupport.RELOAD, location = " nation.action " , type = " redirect " ) })
public class NationAction extends CRUDActionSupport < Nation > {
@Autowired
private NationManager nationManager;
// 基本属性
private Nation entity;
private Long id;
private List < Nation > allNations;
public Nation getModel() {
return entity;
}
@Override
protected void prepareModel() throws Exception {
if (id != null ) {
entity = nationManager.get(id);
} else {
entity = new Nation();
}
}
public void setId(Long id) {
this .id = id;
}
public List < Nation > getAllNations() {
System.out.println( " test " );
return allNations;
}
@Override
public String delete() throws Exception {
try {
nationManager.delete(id);
addActionMessage( " 删除角色成功 " );
} catch (ServiceException e) {
logger.error(e.getMessage(), e);
addActionMessage(e.getMessage());
}
return RELOAD;
}
@Override
public String list() throws Exception {
allNations = nationManager.getAll();
return SUCCESS;
}
@Override
public String input() throws Exception {
System.out.println( " test " );
return INPUT;
}
@Override
public String save() throws Exception {
nationManager.save(entity);
addActionMessage( " 保存用户成功 " );
return RELOAD;
}
}
<
intercept-url
pattern
="/user/nation!save*"
access
="A_MODIFY_ROLE"
/>
< intercept-url pattern ="/user/nation!delete*" access ="A_MODIFY_ROLE" />
< intercept-url pattern ="/user/nation*" access ="A_VIEW_ROLE" />
添加nation.jsp和nation-input.jsp文件
< intercept-url pattern ="/user/nation!delete*" access ="A_MODIFY_ROLE" />
< intercept-url pattern ="/user/nation*" access ="A_VIEW_ROLE" />
<
body
>
< div id ="menu" >
< h3 >
< a href ="${ctx}/user/user.action" > 帐号列表 </ a >
< a href ="${ctx}/user/role.action" > 角色列表 </ a >
< a href ="${ctx}/user/nation.action" > 国籍列表 </ a >
< a href ="${ctx}/j_spring_security_logout" > 退出登录 </ a >
</ h3 >
</ div >
< div id ="message" >< s:actionmessage theme ="mytheme" /></ div >
< div id ="filter" > 你好, <% = SpringSecurityUtils.getCurrentUserName() %> . </ div >
< div id ="listContent" >
< table >
< tr >
< th >< b > 名称 </ b ></ th >
< th >< b > 操作 </ b ></ th >
</ tr >
< s:iterator value ="allNations" >
< tr >
< td > ${name} </ td >
< td >
< security:authorize ifAnyGranted ="A_MODIFY_ROLE" >
< a href ="nation!input.action?id=${id}" > 修改 </ a > 、
< a href ="nation!delete.action?id=${id}" > 删除 </ a >
</ security:authorize >
</ td >
</ tr >
</ s:iterator >
</ table >
</ div >
< div id ="footer" >
< security:authorize ifAnyGranted ="A_MODIFY_ROLE" >
< a href ="nation!input.action" > 增加新国籍 </ a >
</ security:authorize >
</ div >
< div id ="comment" > 本页面为单纯的白板,各式Table Grid组件的应用见Showcase项目(开发中). </ div >
</ body >
< div id ="menu" >
< h3 >
< a href ="${ctx}/user/user.action" > 帐号列表 </ a >
< a href ="${ctx}/user/role.action" > 角色列表 </ a >
< a href ="${ctx}/user/nation.action" > 国籍列表 </ a >
< a href ="${ctx}/j_spring_security_logout" > 退出登录 </ a >
</ h3 >
</ div >
< div id ="message" >< s:actionmessage theme ="mytheme" /></ div >
< div id ="filter" > 你好, <% = SpringSecurityUtils.getCurrentUserName() %> . </ div >
< div id ="listContent" >
< table >
< tr >
< th >< b > 名称 </ b ></ th >
< th >< b > 操作 </ b ></ th >
</ tr >
< s:iterator value ="allNations" >
< tr >
< td > ${name} </ td >
< td >
< security:authorize ifAnyGranted ="A_MODIFY_ROLE" >
< a href ="nation!input.action?id=${id}" > 修改 </ a > 、
< a href ="nation!delete.action?id=${id}" > 删除 </ a >
</ security:authorize >
</ td >
</ tr >
</ s:iterator >
</ table >
</ div >
< div id ="footer" >
< security:authorize ifAnyGranted ="A_MODIFY_ROLE" >
< a href ="nation!input.action" > 增加新国籍 </ a >
</ security:authorize >
</ div >
< div id ="comment" > 本页面为单纯的白板,各式Table Grid组件的应用见Showcase项目(开发中). </ div >
</ body >
<
body
>
< h3 >< s:if test ="id == null" > 创建 </ s:if >< s:else > 修改 </ s:else > 角色 </ h3 >
< div id ="inputContent" >
< form id ="inputForm" action ="nation!save.action" method ="post" >
< input type ="hidden" name ="id" value ="${id}" />
< table >
< tr >
< td > 国籍: </ td >
< td >< input type ="text" name ="name" size ="40" value ="${name}" class ="required" /></ td >
</ tr >
< tr >
< td colspan ="2" >
< input type ="submit" value ="提交" />
< input type ="button" value ="取消" onclick ="history.back()" />
</ td >
</ tr >
</ table >
</ form >
</ div >
</ body >
完成。重新部署、运行,可以执行对国籍的增删改查。
< h3 >< s:if test ="id == null" > 创建 </ s:if >< s:else > 修改 </ s:else > 角色 </ h3 >
< div id ="inputContent" >
< form id ="inputForm" action ="nation!save.action" method ="post" >
< input type ="hidden" name ="id" value ="${id}" />
< table >
< tr >
< td > 国籍: </ td >
< td >< input type ="text" name ="name" size ="40" value ="${name}" class ="required" /></ td >
</ tr >
< tr >
< td colspan ="2" >
< input type ="submit" value ="提交" />
< input type ="button" value ="取消" onclick ="history.back()" />
</ td >
</ tr >
</ table >
</ form >
</ div >
</ body >
虽然最终成功使用,但是其中遇到一个非常恼火的事情。由于定义的表名是dict_nation所以后面相关的名称都是DictNation和dictNation,结果新增的功能无法实现,怀疑是违反了某个框架的命名规范,导致无法实现,有空再深入学习ss3。