ssh框架 添删改查及excel下载例子(lib里的包已经清空)

ssh框架

第一步:添加spring  添加hibernate  注:Spirng applicationContext.xml 文件 放在目mytest\WebRoot\WEB-  INF 下

----------------------------------------------------------------------------------------------------------------------------------

第二步:配置 web.xml 如下(固定)

----------------------------------------------------------------------------------------------------------------------------------

 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
   <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
 </filter>

 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
  <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>
</web-app>

----------------------------------------------------------------------------------------------------------------------------------

第三步:配置 mytest\src\struts.xml 文件

----------------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
 <package name="user" extends="struts-default">
  
  <action name="saveUser" class="saveUserAction">
   <result name="success" type="redirect">listUser.action</result>
   <result name="input">/save.jsp</result>
  </action>
  
  <action name="listUser" class="listUserAction">
   <result>/list.jsp</result>
  </action>
  
  <action name="deleteUser" class="removeUserAction">
   <result name="success" type="redirect">listUser.action</result>
  </action>
  
  <action name="updatePUser" class="updatePUserAction">
   <result name="success">/update.jsp</result>
  </action>
  
  <action name="updateUser" class="updateUserAction">
   <result name="success" type="redirect">listUser.action</result>
   <result name="input">/update.jsp</result>
  </action>
  
  <action name="generateExcel" class="generateExcelAction">
   <result name="success" type="stream">
    <param name="contentType">application/vnd.ms-excel</param>
    <param name="contentDisposition">filename="AllUsers.xls"</param>
    <param name="inputName">downloadFile</param>
   </result>
  </action>
  
 </package>
</struts>

----------------------------------------------------------------------------------------------------------------------------------

第四步:配置 applicationContext.xml 文件

----------------------------------------------------------------------------------------------------------------------------------

 

<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
 <property name="url" value="jdbc:mysql://localhost:3306/mytest"></property>
 <property name="username" value="root"></property>
 <property name="password" value="root"></property>
 <property name="maxActive" value="100"></property>
 <property name="maxIdle" value="30"></property>
 <property name="maxWait" value="500"></property>
 <property name="defaultAutoCommit" value="true"></property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 <property name="dataSource" ref="dataSource"></property>
 <property name="hibernateProperties">
  <props>
   <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
   <prop key="hibernate.show_sql">true</prop>
  </props>
 </property>
 <property name="mappingResources">
  <list>
   <value>com/test/bean/User.hbm.xml</value>
  </list>
 </property>
</bean>

<bean id="userDao" class="com.test.dao.impl.UserDAOImpl" scope="singleton">
 <property name="sessionFactory">
  <ref bean="sessionFactory"/>
 </property>
</bean>

<bean id="userService" class="com.test.service.impl.UserServiceImpl">
 <property name="userDao" ref="userDao"></property>
</bean>

<bean id="saveUserAction" class="com.test.action.user.SaveUserAction" scope="prototype">
 <property name="service" ref="userService"></property>
</bean>

<bean id="listUserAction" class="com.test.action.user.ListUserAction" scope="prototype">
 <property name="service" ref="userService"></property>
</bean>

<bean id="removeUserAction" class="com.test.action.user.RemoveUserAction" scope="prototype">
 <property name="service" ref="userService"></property>
</bean>

<bean id="updatePUserAction" class="com.test.action.user.UpdatePUserAction" scope="prototype">
 <property name="service" ref="userService"></property>
</bean>

<bean id="updateUserAction" class="com.test.action.user.UpdateUserAction" scope="prototype">
 <property name="service" ref="userService"></property>
</bean>

<bean id="generateExcelAction" class="com.test.action.user.GenerateExcelAction" scope="singleton">
 <property name="service" ref="userService"></property>
</bean>

</beans

----------------------------------------------------------------------------------------------------------------------------------

第五步:Dao接口

----------------------------------------------------------------------------------------------------------------------------------

package com.test.dao;

import java.util.List;

import com.test.bean.User;

public interface UserDAO
{
 public boolean saveUser(User user);

 public void removeUser(User user);

 public User findUserById(Integer id);

 public List<User> findAllUsers();

 public void updateUser(User user);
}

----------------------------------------------------------------------------------------------------------------------------------

第六步:实现dao接口

----------------------------------------------------------------------------------------------------------------------------------

package com.test.dao.impl;

import java.util.List;

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

import com.test.bean.User;
import com.test.dao.UserDAO;

public class UserDAOImpl extends HibernateDaoSupport implements UserDAO
{


 @SuppressWarnings("unchecked")
 public List<User> findAllUsers()
 
 {
 
  String hql = "from User user order by user.id desc";
  return (List<User>)this.getHibernateTemplate().find(hql);
 }

 public User findUserById(Integer id)
 {
  User user = (User) this.getHibernateTemplate().get(User.class, id);
  return user;
 }

 public void removeUser(User user)
 {
  this.getHibernateTemplate().delete(user);
 }

 public boolean saveUser(User user)
 {
  this.getHibernateTemplate().save(user);
  return true;
 }

 public void updateUser(User user)
 {
  this.getHibernateTemplate().update(user);
 }

}

 

----------------------------------------------------------------------------------------------------------------------------------

第七步:servise接口

----------------------------------------------------------------------------------------------------------------------------------

package com.test.service;

import java.io.InputStream;
import java.util.List;

import com.test.bean.User;

public interface UserService
{
 public List<User> findAll();

 public void save(User user);

 public void delete(User user);

 public User findById(Integer id);

 public void update(User user);
 
 public InputStream getInputStream();
}

 

----------------------------------------------------------------------------------------------------------------------------------

第七步:实现servise接口

----------------------------------------------------------------------------------------------------------------------------------

package com.test.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.test.bean.User;
import com.test.dao.UserDAO;
import com.test.service.UserService;

public class UserServiceImpl implements UserService
{
 private UserDAO userDao;

 public UserDAO getUserDao()
 {
  return userDao;
 }

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

 public void delete(User user)
 {
  this.userDao.removeUser(user);
 }

 public List<User> findAll()
 {
  return this.userDao.findAllUsers();
 }

 public User findById(Integer id)
 {
  return this.userDao.findUserById(id);
 }

 public void save(User user)
 {
  boolean brt = this.userDao.saveUser(user);
  if (brt) {
   //zhibiao
  }
 }

 public void update(User user)
 {
  this.userDao.updateUser(user);
 }

 public InputStream getInputStream()
 {
  HSSFWorkbook wb = new HSSFWorkbook();
  HSSFSheet sheet = wb.createSheet("sheet1");

  HSSFRow row = sheet.createRow(0);

  HSSFCell cell = row.createCell((short) 0);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("序号");

  cell = row.createCell((short) 1);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("姓");

  cell = row.createCell((short) 2);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("名");

  cell = row.createCell((short) 3);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("年龄");

  List<User> list = this.findAll();

  for (int i = 0; i < list.size(); ++i)
  {
   User user = list.get(i);

   row = sheet.createRow(i + 1);

   cell = row.createCell((short) 0);
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   cell.setCellValue(i + 1);

   cell = row.createCell((short) 1);
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   cell.setCellValue(user.getFirstname());

   cell = row.createCell((short) 2);
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   cell.setCellValue(user.getLastname());

   cell = row.createCell((short) 3);
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   cell.setCellValue(user.getAge());
  }

  File file = new File("test.xls");

  try
  {
   OutputStream os = new FileOutputStream(file);
   wb.write(os);
   os.close();
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }

  InputStream is = null;
  try
  {
   is = new FileInputStream(file);
  }
  catch (FileNotFoundException e)
  {
   e.printStackTrace();
  }

  return is;

 }

}
----------------------------------------------------------------------------------------------------------------------------------

第九步:添

----------------------------------------------------------------------------------------------------------------------------------
  jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    <title>Save User</title>
   
  </head>
 
  <body>
  
    <h1><font color="red">Save User</font></h1>
   
    <s:form action="saveUser">
     <s:textfield name="user.firstname" label="%{getText('firstname')}"></s:textfield>
     <s:textfield name="user.lastname" label="%{getText('lastname')}"></s:textfield>
     <s:textfield name="user.age" label="%{getText('age')}"></s:textfield>
  <s:submit></s:submit>
     
    </s:form>
  
  </body>
</html>


 action 文件

package com.test.action.user;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.User;
import com.test.service.UserService;

public class SaveUserAction extends ActionSupport
{
 private User user;
 private UserService service;

 public User getUser()
 {
  return user;
 }

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

 public UserService getService()
 {
  return service;
 }

 public void setService(UserService service)
 {
  this.service = service;
 }

 @Override
 public String execute() throws Exception
 {
  this.service.save(this.user);

  return SUCCESS;
 }

 @Override
 @SuppressWarnings("unchecked")
 public void validate()
 {
  Map map = this.getFieldErrors();
  Set set = map.keySet();

  for (Iterator iter = set.iterator(); iter.hasNext();)
  {
   System.out.println(map.get(iter.next()));
  }
 }
}

----------------------------------------------------------------------------------------------------------------------------------

第十步: 查 改 删

----------------------------------------------------------------------------------------------------------------------------------

显示jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    <title>My JSP 'list.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

 <script type="text/javascript">
 function del()
 {
  if(confirm("你真的想删除该记录么?"))
  {
   return true;
  }
  return false;
 }
 </script>

  </head>
 
  <body>
  
   <h1><font color="red"><center>Users List</center></font></h1>
  
   <table border="1" width="80%" align="center">
  
    <tr>
     <td>序号
     </td>
     
     <td>姓
     </td>
     
     <td>名
     </td>
     
     <td>年龄
     </td>
     
     <td>删除
     </td>
     
     <td>更新
     </td>
    
    </tr>
    
    <s:iterator value="#request.list" id="us">
     <tr>
      <td><s:property value="#us.id"/>
      </td>
      
      <td><s:property value="#us.firstname"/>
      </td>
      
      <td><s:property value="#us.lastname"/>
      </td>
      
      <td><s:property value="#us.age"/>
      </td>
      
      <td><s:a href="deleteUser.action?user.id=%{#us.id}" onclick="return del();">delete</s:a>
      </td>
      
      <td><s:a href="updatePUser.action?user.id=%{#us.id}">update</s:a>
      </td>
     
     
     
     </tr>
    </s:iterator>
    
    </table>
  
   <s:a href="index.jsp">Homepage</s:a><br><br>
   <s:a href="generateExcel.action">生成excel</s:a>
  
  </body>
</html>

 

修改之前必须利用Id先把相关数据查出来 jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'update.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
   
    <h1><font color="red">Update User</font></h1>
   
    <s:form action="updateUser">
     
     <table>
      <tr>
       <td>
        <s:hidden name="user.id" value="%{user.id}"></s:hidden>
       </td>
      </tr>
      
      <tr>
       <td>
        <s:textfield name="user.firstname" value="%{user.firstname}" label="%{getText('firstname')}"></s:textfield>
       </td>
      </tr>
      
      <tr>
       <td>
        <s:textfield name="user.lastname" value="%{user.lastname}" label="%{getText('lastname')}"></s:textfield>
       </td>
      </tr>
      
      <tr>
       <td>
        <s:textfield name="user.age" value="%{user.age}" label="%{getText('age')}"></s:textfield>
       </td>
      </tr>
      
      <tr>
       <td>
        <s:submit></s:submit>
       </td>
      </tr>
     </table>
     
    </s:form>
   
  </body>
</html>
----------------------------------------------------------------------------------------------------------------------------------

第十一步: 查 删 改 Action

----------------------------------------------------------------------------------------------------------------------------------

package com.test.action.user;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.test.service.UserService;

public class ListUserAction extends ActionSupport
{
 private UserService service;

 public UserService getService()
 {
  return service;
 }

 public void setService(UserService service)
 {
  this.service = service;
 }

 @SuppressWarnings("unchecked")
 @Override
 public String execute() throws Exception
 {
  Map request = (Map) ActionContext.getContext().get("request");
  
  request.put("list", service.findAll());

  return SUCCESS;
 }
}

 

package com.test.action.user;

import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.User;
import com.test.service.UserService;

public class RemoveUserAction extends ActionSupport
{
 private User user;
 private UserService service;
 
 public User getUser()
 {
  return user;
 }
 public void setUser(User user)
 {
  this.user = user;
 }
 public UserService getService()
 {
  return service;
 }
 public void setService(UserService service)
 {
  this.service = service;
 }
 
 @Override
 public String execute() throws Exception
 {
  this.service.delete(user);
  
  return SUCCESS;
 }
 
}

   先查

package com.test.action.user;

import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.User;
import com.test.service.UserService;

public class UpdatePUserAction extends ActionSupport
{
 private User user;
 private UserService service;
 
 public User getUser()
 {
  return user;
 }
 public void setUser(User user)
 {
  this.user = user;
 }
 public UserService getService()
 {
  return service;
 }
 public void setService(UserService service)
 {
  this.service = service;
 }
 
 @Override
 public String execute() throws Exception
 {
  user = this.service.findById(user.getId());
  
  return SUCCESS;
 }
}

再改

package com.test.action.user;

import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.User;
import com.test.service.UserService;

public class UpdateUserAction extends ActionSupport
{
 private User user;
 private UserService service;

 public User getUser()
 {
  return user;
 }

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

 public UserService getService()
 {
  return service;
 }

 public void setService(UserService service)
 {
  this.service = service;
 }
 
 @Override
 public String execute() throws Exception
 {
  
  this.service.update(user);
 
  return SUCCESS;
 }

}

----------------------------------------------------------------------------------------------------------------------------------

第十二步:excle 文件下载

----------------------------------------------------------------------------------------------------------------------------------
package com.test.action.user;

import java.io.InputStream;

import com.opensymphony.xwork2.ActionSupport;
import com.test.service.UserService;

public class GenerateExcelAction extends ActionSupport
{
 private UserService service;

 public UserService getService()
 {
  return service;
 }

 public void setService(UserService service)
 {
  this.service = service;
 }
 
 public InputStream getDownloadFile()
 {
  return this.service.getInputStream();
 }
 
 @Override
 public String execute() throws Exception
 {
  return SUCCESS;
 }
}

 

你可能感兴趣的:(框架,bean,struts,ssh,Excel)