Ubuntu开发Struts2应用(7、完成增删查改操作)

2012年02月14日 JAVA, Linux, Ubuntu 暂无评论

由于注重的是整合,所以这里没有写得很完善,没有事务处理,也没有处理业务逻辑的service层等,更适合初学者学习。

1、接口UserDAO以及实现类UserDAOImpl。主要是hibernate和spring的整合,提供数据增删查改的操作。

UserDAO.java

package com.linuxsight.dao;

import java.util.List;
public interface UserDAO {

public void insert(Object o) throws Exception;

public List<Object> list() throws Exception;

public void update(Object o) throws Exception;

public void delete(int id) throws Exception;

public Object queryById(int id) throws Exception;

}
UserDAOImpl.java
package com.linuxsight.daoimpl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.linuxsight.dao.UserDAO;
import com.linuxsight.vo.User;

public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {

 @Override
 public void insert(Object o) throws Exception {
  this.getHibernateTemplate().save(o);
 }

 @Override
 public List<Object> list() throws Exception {
 return this.getHibernateTemplate().find("from User");
 }

 @Override
 public void update(Object o) throws Exception {
  this.getHibernateTemplate().update(o);
 }

 @Override
 public void delete(int id) throws Exception {
  this.getHibernateTemplate().delete(this.queryById(id));
 }

 @Override
 public Object queryById(int id) throws Exception {
  return this.getHibernateTemplate().get(User.class, id);
 }

}

UserAction.java

package com.linuxsight.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.linuxsight.dao.UserDAO;
import com.linuxsight.vo.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
 private static final long serialVersionUID = 1L;
 private User user;
 public User getUser() {
  return user;
 }

 public void setUser(User user) {
  this.user = user;
 }

 public UserDAO getUserDao() {
  return userDao;
 }

 public void setUserDao(UserDAO userDao) {
  this.userDao = userDao;
 }

 private UserDAO userDao;
 //插入
 public String insert() throws Exception
 {
  this.userDao.insert(user);
  return SUCCESS;
 }
 //列出
 public String list() throws Exception{
  ActionContext.getContext().put("users", this.userDao.list());
  return "list";
 }
//进入更新页面
 public String goUpdate() throws Exception{
  HttpServletRequest request=ServletActionContext.getRequest();
  int id=Integer.parseInt(request.getParameter("id"));
  user=(User)this.userDao.queryById(id);
  return "update";
 }
 //更新
 public String update() throws Exception{
  this.userDao.update(user);
  return SUCCESS;
 }
 //删除
 public String delete() throws Exception{
  HttpServletRequest request=ServletActionContext.getRequest();
  int id=Integer.parseInt(request.getParameter("id"));
  this.userDao.delete(id);
  return SUCCESS;
 }

 @Override
 public String execute() throws Exception
 {
  return SUCCESS;
 }

}

2、这里的class="userAction"对应applicationContext.xml的bean id="userAction"

struts.xml的action name="user_*"采用通配符,所以本项目对应的访问地址应该是

localhost:8080/s2sh/user_list

spring用的是单例 而struts2每次请求要生成新的  所以加上 scope=prototype以免造成线程不安全

struts.xml

<package name="user"  extends="struts-default">
<action name="user_*" method="{1}">
<result name="success" type="redirect">/user_list.action</result>
<result name="list">/list.jsp</result>
<result name="update">/update.jsp</result>
</action>
</package>

applicationContext.xml

<bean id="userDao"
scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

3、列出页面:这里特意分了2种书写方式:JSTL和ONGL。ONGL是另一种表达式语言,通过列出数据循环迭代来比较2者的写法。

list.jsp

JSTL/EL:
<br/>
<c:forEach items="${users}" var="user">
${user.name}, ${user.age},
<a href="user_goUpdate?id=${user.id }">更新</a>&nbsp;&nbsp;
<a href="user_delete?id=${user.id }">删除</a><br/>
</c:forEach>
<br/>

ONGL:
<br/>
<s:iterator value="#request.users">
<s:property value="name"/> ,<s:property value="age"/>,
<a href="user_goUpdate?id=<s:property value='id'/>">更新</a>
&nbsp;&nbsp;       <a href="user_delete?id=<s:property value='id'/>">删除</a>
<br/>
</s:iterator>

<br/>

4、插入与更新页面,主要掌握struts2 form标签的使用

insert.jsp

 <s:form action="user_insert" method="post">
    <s:textfield name="user.name" label="名字"></s:textfield>
    <s:textfield name="user.age" label="年龄"></s:textfield>
 <s:submit></s:submit>
  </s:form>

update.jsp

 <h3><font color="blue">更新用户|Struts2整合Hibernate、Spring|Linux视野</font></h3>
    <s:form action="user_update" method="post">
     <s:textfield name="user.name" label="姓名" value="%{user.name }"></s:textfield>
     <s:textfield name="user.age" label="年龄" value="%{user.age}"></s:textfield>
    <s:hidden name="user.id" value="%{user.id }"></s:hidden>
  <s:submit></s:submit>
    </s:form>

列出页面如下:

我们查看一下数据库,记得是

mysql --default-character-set=utf8 -uroot -p123456

这样就不会看到乱码了

最后看下整个项目

最后是整个SSH2项目的源代码,下载地址:http://115.com/file/anw3ci3y
Linuxs视野:Ubuntu开发Struts2应用-项目源代码-s2sh.zip

给我留言