structs +hibernate 通过Action 将前台数据存入数据库 , 表单提交

逻辑:前台jsp中的form,调用action中的add方法,action中通过定义和页面元素相同的私有变量(生成get、set方法可以拿到前台变量),然后调用dao中的hibernate保存

1、struts.xml

采用通配符配置
  <action name="test_*" class="com.action.TestAction" method="{1}">
   <result name="add_sucess">/test/addTestSucess.jsp</result>
   <result name="search_sucess">/test/searchTest.jsp</result>
  </action>

2、前台页面 addTest.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<body style="background:#F5F5F5">
<div>
<ol class="breadcrumb">
  <li class="active">测试</a></li>
  <li class="active">添加</li>
</ol>
<div id="content">
<form action="test_add.action" method="post" id="test">
 <table class="table table-bordered" border="0" width="100%" align="center">
  <tr>
   <td style="width: 270px; height: 18px;">
   a:
   </td>
   <td style="width: 500px; height: 18px;">
     <input type="text" name="a" class="form-control input-sm" style="width:300px" placeholder="a" value="">
   </td>
   <td style="width: 270px; height: 18px;">
    b:
   </td>
   <td style="width: 500px; height: 18px;">
     <input type="text" name="b" class="form-control input-sm" style="width:300px" placeholder="b" value="">
   </td>
  </tr>
  <tr>
   <td style="width: 270px; height: 18px;">
     c:
   </td>
   <td style="width: 500px; height: 18px;">
     <input type="text" name="c" class="form-control input-sm" style="width:300px" placeholder="c" value="">
   </td>
   
  </tr>
 
 </table>
 <table align="center">
   <center>
   <div class="btn-group" style="text-align:center">
   <td style="width: 200px;">
    <input type="submit" class="btn btn-info btn-lg" value="提交">
   </td>
   <td style="width: 200px;">
    <button type="button" class="btn btn-info btn-lg">重置</button>
   </td>
   </div>
   </center>
 </table>
 
 </form>
</div>
</div>
</body>
</html>

3、TestAction 

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.bean.TestPortal;
import com.service.TestService;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 测试action
 * @author anjl
 *
 */
public class TestAction extends ActionSupport {
 
 private static final long serialVersionUID = 1L;
 @Autowired
 private TestService testService;
 
 private String a="";
 private String b="";
 private String c="";
 /*private int page =1;
 private String op;*/
 
 private List<TestPortal> list;
 
 public List<TestPortal> getList() {
  return list;
 }
 public void setList(List<TestPortal> list) {
  this.list = list;
 }
 public String getA() {
  return a;
 }
 public void setA(String a) {
  this.a = a;
 }
 public String getB() {
  return b;
 }
 public void setB(String b) {
  this.b = b;
 }
 public String getC() {
  return c;
 }
 public void setC(String c) {
  this.c = c;
 }
 public String add() {
  System.out.println("==add===="+getA());
  TestPortal t = new TestPortal(getA(),getB(),getC());
  testService.add(t);
  return "add_sucess";
 }
 
 public String search(){
 
  /*if(op!=null&&op.equals("first")){
   page=1;
  }else if(op!=null&&op.equals("before")){
   page = page-1;
  }else if(op!=null&&op.equals("after")){
   page = page+1;
  }*/
 
  System.out.println("==========search=========");
  list = testService.search(getA(), getB(), getC());
  return "search_sucess";
 }
}

4、bean  TestPortal  和数据表对应

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="testPortal")
public class TestPortal implements Serializable{
 private static final long serialVersionUID = 1L;
 public TestPortal(){
 }
 
 public TestPortal(String a,String b,String c){
  this.a=a;
  this.b=b;
  this.c=c;
 }
 
 @Id
 @GeneratedValue(strategy=GenerationType.AUTO)
 @Column(name = "id")
 private Long id;
 
 @Column(name = "a")
 private String a;
 @Column(name = "b")
 private String b;
 @Column(name = "c")
 private String c;
 
 
 public Long getId() {
  return id;
 }
 public void setId(Long id) {
  this.id = id;
 }
 public String getA() {
  return a;
 }
 public void setA(String a) {
  this.a = a;
 }
 public String getB() {
  return b;
 }
 public void setB(String b) {
  this.b = b;
 }
 public String getC() {
  return c;
 }
 public void setC(String c) {
  this.c = c;
 }
}

5、service层和dao层

主要是dao层,service没做处理
(1)、TestDaoImpl 
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.haier.bean.TestPortal;
import com.haier.dao.TestDao;
@Repository("testDao")
public class TestDaoImpl implements TestDao {
 private HibernateTemplate hibernateTemplate;
 public HibernateTemplate getHibernateTemplate() {
  return hibernateTemplate;
 }
 @Resource
 public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
  this.hibernateTemplate = hibernateTemplate;
 }
 @Override
 public void add(TestPortal t) {
  Session session = this.getHibernateTemplate().getSessionFactory()
    .getCurrentSession();
  session.save(t);
 }
 @Override
 public List<TestPortal> search(String a, String b, String c) {
 
  Session session = this.getHibernateTemplate().getSessionFactory()
    .getCurrentSession();
  StringBuffer hql = new StringBuffer("from TestPortal testPortal where 1=1 ");
  if(!"".equals(a) && null!=a){
   hql.append(" and a=‘"+a+"‘ ");
  }
  if(!"".equals(b) && null!=b){
   hql.append(" and b=‘"+b+"‘ ");
  }
  if(!"".equals(c) && null!=c){
   hql.append(" and c=‘"+c+"‘ ");
  }
  List<TestPortal> list =null;
  System.out.println("search hql : "+hql.toString());
  Query query = session.createQuery(hql.toString());
  list = query.list();
  return list;
 }
}

(2)、service 没做处理
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gp.dao.UserDao;
import com.haier.bean.TestPortal;
import com.haier.dao.TestDao;
import com.haier.service.TestService;
@Service("testService")
public class TestServiceImpl implements TestService{
 
 private TestDao testDao;
 public TestDao getTestDao() {
  return testDao;
 }
 @Resource(name="testDao")
 public void setTestDao(TestDao testDao) {
  this.testDao = testDao;
 }
 @Override
 public void add(TestPortal t) {
  testDao.add(t);
 }
 @Override
 public List<TestPortal> search(String a, String b, String c) {
  List<TestPortal> list = testDao.search(a, b, c);
  return list;
 }
}



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。