锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
榪欓噷鐢熸垚Excel鐢ㄧ殑鏄疨OI鐨凙PI
WebWork涓畾涔塕esultType瑙嗗浘綾誨瀷鍙渶瑕佺戶鎵縍esult鎺ュ彛
浠g爜濡備笅
package com.customer.resulttype;
import com.opensymphony.xwork.Result;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.webwork.ServletActionContext;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
public class ExcelResult implements Result{
聽聽聽 private HSSFWorkbook workbook;
聽聽聽 private String filename;
聽聽聽 private String contenttype;
聽聽聽 public void execute(ActionInvocation invocation) throws Exception {
聽聽聽聽聽聽聽 if(contenttype==null)
聽聽聽聽聽聽聽聽聽聽聽 contenttype = "application/ms-excel";
聽聽聽聽聽聽聽 if (workbook==null)
聽聽聽聽聽聽聽聽聽聽聽 workbook = (HSSFWorkbook) invocation.getStack().findValue("workbook");
聽聽聽聽聽聽
聽聽聽聽聽聽聽 HttpServletResponse response = ServletActionContext.getResponse();
聽聽聽聽聽聽聽 response.setContentType(contenttype);
聽聽聽聽聽聽聽 response.setHeader("Content-Disposition","attachment;Filename="+filename+".xls");
聽聽聽聽聽聽聽 OutputStream os = response.getOutputStream();
聽聽聽聽聽聽聽 workbook.write(os);
聽聽聽聽聽聽聽 os.flush();
聽聽聽聽聽聽聽 os.close();
聽聽聽 }
聽聽聽 public void setWorkbook(HSSFWorkbook workbook) {
聽聽聽聽聽聽聽 this.workbook = workbook;
聽聽聽 }
聽聽聽 public void setFilename(String filename) {
聽聽聽聽聽聽聽 this.filename = filename;
聽聽聽 }
聽聽聽 public void setContenttype(String contenttype) {
聽聽聽聽聽聽聽 this.contenttype = contenttype;
聽聽聽 }
}
瑙嗗浘鍋氬畬涔嬪悗鍋氬涓嬮厤緗繍琛屾祴璇?br />
package com.customer.action;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.webwork.ServletActionContext;
import com.dboperate.ResultGather;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Map;
public class ExportExcelAction extends ActionSupport {
聽聽聽 private HSSFWorkbook workbook;
聽聽聽 public String execute() throws Exception {
聽聽聽聽聽聽聽 return SUCCESS;
聽聽聽 }
聽聽聽 public String product() throws Exception {
聽聽聽聽聽聽聽 try {
聽聽聽聽聽聽聽聽聽聽聽 workbook = new HSSFWorkbook();
聽聽聽聽聽聽聽聽聽聽聽 HSSFSheet sheet = workbook.createSheet();
聽聽聽聽聽聽聽聽聽聽聽 workbook.setSheetName(0, "鍘傚晢浜у搧", (short) 1);
聽聽聽聽聽聽聽聽聽聽聽 HSSFRow row = sheet.createRow((short) 0);
聽聽聽聽聽聽聽聽聽聽聽 HSSFCell cell0 = row.createCell((short) 0);
聽聽聽聽聽聽聽聽聽聽聽 HSSFCell cell1 = row.createCell((short) 1);
聽聽聽聽聽聽聽聽聽聽聽 HSSFCell cell2 = row.createCell((short) 2);
聽聽聽聽聽聽聽聽聽聽聽 HSSFCell cell3 = row.createCell((short) 3);
聽聽聽聽聽聽聽聽聽聽聽 HSSFCell cell4 = row.createCell((short) 4);
聽聽聽聽聽聽聽聽聽聽聽 HSSFCell cell5 = row.createCell((short) 5);
聽聽聽聽聽聽聽聽聽聽聽 HSSFCell cell6 = row.createCell((short) 6);
聽聽聽聽聽聽聽聽聽聽聽 HSSFCell cell7 = row.createCell((short) 7);
聽聽聽聽聽聽聽聽聽聽聽 HSSFCell cell8 = row.createCell((short) 8);
聽聽聽聽聽聽聽聽聽聽聽 HSSFCell cell9 = row.createCell((short) 9);
聽聽聽聽聽聽聽聽聽聽聽 cell0.setEncoding(HSSFCell.ENCODING_UTF_16);//榪欓噷鏄緗紪鐮佷繚璇佷腑鏂囨甯告樉紺?br />聽聽聽聽聽聽聽聽聽聽聽 cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
聽聽聽聽聽聽聽聽聽聽聽 cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
聽聽聽聽聽聽聽聽聽聽聽 cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
聽聽聽聽聽聽聽聽聽聽聽 cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
聽聽聽聽聽聽聽聽聽聽聽 cell5.setEncoding(HSSFCell.ENCODING_UTF_16);
聽聽聽聽聽聽聽聽聽聽聽 cell6.setEncoding(HSSFCell.ENCODING_UTF_16);
聽聽聽聽聽聽聽聽聽聽聽 cell7.setEncoding(HSSFCell.ENCODING_UTF_16);
聽聽聽聽聽聽聽聽聽聽聽 cell8.setEncoding(HSSFCell.ENCODING_UTF_16);
聽聽聽聽聽聽聽聽聽聽聽 cell9.setEncoding(HSSFCell.ENCODING_UTF_16);
聽聽聽聽聽聽聽聽聽聽聽 cell0.setCellValue("鍘傚晢鍚?);
聽聽聽聽聽聽聽聽聽聽聽 cell1.setCellValue("浜у搧鍚?);
聽聽聽聽聽聽聽聽聽聽聽 cell2.setCellValue("閲嶉噺");
聽聽聽聽聽聽聽聽聽聽聽 cell3.setCellValue("鏄熺駭");
聽聽聽聽聽聽聽聽聽聽聽 cell4.setCellValue("parama");
聽聽聽聽聽聽聽聽聽聽聽 cell5.setCellValue("paramb");
聽聽聽聽聽聽聽聽聽聽聽 cell6.setCellValue("paramc");
聽聽聽聽聽聽聽聽聽聽聽 cell7.setCellValue("paramd");
聽聽聽聽聽聽聽聽聽聽聽 cell8.setCellValue("鐘舵?);
聽聽聽聽聽聽聽聽聽聽聽 cell9.setCellValue("澶囨敞");
聽聽聽聽聽聽聽 } catch (Exception e) {
聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽 return SUCCESS;
聽聽聽 }
聽聽聽 public HSSFWorkbook getWorkbook() {
聽聽聽聽聽聽聽 return workbook;
聽聽聽 }
}
Xwork.xml涓厤緗姞鍏?br />
聽聽聽聽聽聽聽 <result-type default="true" name="freemarker"
聽聽聽聽聽聽聽聽聽聽聽 <result-type name="excel" class="com.customer.resulttype.ExcelResult"/>
聽聽聽聽聽聽聽 </result-types>
聽<action name="exportExcel" class="com.customer.action.ExportExcelAction">
聽聽聽聽聽聽聽聽聽聽聽 <result name="success" type="excel">
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 <param name="filename">productparam>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽 </result>
聽聽聽聽聽聽聽 </action>
聽聽聽 public String update() throws Exception {
聽聽聽聽聽聽聽 ....
聽聽聽 }
聽聽聽 public String delete() throws Exception {
聽聽聽聽聽聽聽 ....
聽聽聽 }
聽聽聽 public String list() throws Exception {
聽聽聽聽聽聽聽 ....
聽聽聽 }
DAO
涓嬮潰鏄疍AO鎺ュ彛
public interface BaseDao {
聽聽聽 public int insert(Object form);
聽聽聽 public int update(Object form);
聽聽聽 public int delete(Object form);
聽聽聽 public int delete(int[] id);//榪欓噷鏄負浜嗘敮鎸佹壒閲忓垹闄?br />聽聽聽 public Map getInfo(Object form);
聽聽聽 public List getList();
聽聽聽 public void getList(Pagination page);//榪欓噷娌℃湁榪斿洖鍊鹼紝鍐呰銆傘傘傘?br />}
EXTENDS
鎵╁睍閮ㄥ垎
1 BaseAction
榪欓噷澧炲姞浜嗕竴浜涘叕鐢ㄦ柟渚跨殑鍔熻兘錛屼究浜庡叾浠朅CTION緇ф壙錛屽叡鐢?br />2 DaoSupport
榪欓噷涔熸槸涓浜涘叕鐢ㄧ殑鍔熻兘錛孌ao涓敤鐨勫ぇ閮ㄥ垎SQL閮芥槸閰嶇疆鍦ㄥ閮ㄩ厤緗枃浠朵腑
3DaoFactory
public class DaoFactory {
聽聽聽 public static BaseDao getDao(String daoKey){
聽聽聽聽聽聽聽 if("user".equals(daoKey)) //榪欐牱鐨勬柟寮忓彲浠ラ氳繃鍦╔WORK.XML涓厤緗瓵CTION浣跨敤鍝釜DAO錛堥潤鎬佹敞鍏ワ級
聽聽聽聽聽聽聽聽聽聽聽 return new UserDao();
聽聽聽聽聽聽聽 else
聽聽聽聽聽聽聽聽聽聽聽 return null;
聽聽聽 }
}
鏁版嵁搴撴槸MYSQL5
CREATE TABLE `usertbl` (聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽 `User_ID` int(11) NOT NULL auto_increment,聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽 `username` varchar(50) default NULL,聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽 `password` varchar(50) default NULL,聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽 `truename` varchar(50) default NULL,聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽 `usersex` tinyint(4) default NULL,聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽 `useremail` varchar(50) default NULL,聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽 PRIMARY KEY聽 (`User_ID`)聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽 )
婧愮爜涓嬭澆鍦板潃
http://www.migti.com/download.ww
瀛︿範鐨勬椂鍊欑湅浜嗙綉涓婄殑涓浜涢厤緗暀紼嬶紝娌℃湁杈懼埌鎯寵鐨勭粨鏋?BR>鏌ヤ簡濂藉璧勬枡鎵嶅紕鍑烘潵錛岄殢鍚庡悗鑷繁鏁寸悊鍑轟竴涓厤緗柟娉?BR>
web.xml
// 榪欓噷涓嶉渶瑕侀厤緗瓧絎﹁繃婊わ紝緗戜笂鏈夌殑渚嬪瓙鍔犱簡錛屽疄闄呬笂
webwork.properties閲岃緗涓嬪氨鍙互浜嗛〉闈篃鏄疓BK
webwork.locale=zh_CN
webwork.i18n.encoding=GBK
錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛?BR><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.atlassian.xwork.ext.ResolverSetupServletContextListener</listener-class>
</listener>
<!--
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
-->
<servlet>
<servlet-name>webwork</servlet-name>
<servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>com.opensymphony.webwork.views.freemarker.FreemarkerServlet</servlet-class>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webwork</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>freemarker</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>webwork</taglib-uri>
<taglib-location>/WEB-INF/webwork.tld</taglib-location>
</taglib>
</web-app>
錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛?BR>
xwork.xml
==================---------------------------------------------
<?xml version="1.0"?>
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
<include file="webwork-default.xml"/>
<package name="users" extends="webwork-default"
externalReferenceResolver="com.atlassian.xwork.ext.SpringServletContextReferenceResolver">
<interceptors>
<interceptor name="reference-resolver" class="com.opensymphony.xwork.interceptor.ExternalReferencesInterceptor"/>
<interceptor-stack name="myDefaultWebStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="reference-resolver"/>
<interceptor-ref name="model-driven"/>
<interceptor-ref name="params"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myDefaultWebStack"/>
<action name="blogUser" class="com.jsblog.action.BlogUserAction">
<external-ref name="baseDao">baseDaoTarget</external-ref> //榪欓噷鏄妸applicationContext閲岄厤緗殑DAO 娉ㄥ叆action閲?action閲岃鏈塨aseDao灞炴?BR> <result name="success">/add.htm</result>
</action>
-------------------------------------------------------------------------
applicationContext.xml
---------------------------------------------------------------------------
<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="no" default-dependency-check="none" default-lazy-init="false">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=jsblog;SelectMethod=cursor</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value>jfy</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>com/jsblog/BlogUserForm.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
net.sf.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="baseDaoTarget" class="com.jsblog.dao.BlogUserDao">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
</beans>
---------------------------------------------------------------------------
BlogUserDao.java
---------------------------------------------------------------------------
package com.jsblog.dao;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
import org.springframework.orm.hibernate.HibernateCallback;
import org.springframework.orm.hibernate.SessionFactoryUtils;
import com.jsblog.BlogUserForm;
import java.io.Serializable;
import java.util.List;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
public class BlogUserDao extends HibernateDaoSupport implements BaseDao {
public void insert(BlogUserForm bloguser) {
getHibernateTemplate().save(bloguser);
}
}
絎竴縐嶇敤ActionContext綾伙紝鎵鏈夌殑鍙傛暟閮戒粠榪欓噷ActionContext.getContext().getParameters()鍙?BR>浠栬繑鍥炵殑鏄竴涓狹ap綾誨瀷
Map param= ActionContext.getContext().getParameters();
濡傛灉鏈変竴涓彁浜よ繃鏉ョ殑username
閭e氨鍙互榪欐牱鍐?BR>param.get("username");涓嶈繃榪欓噷瑕佹敞鎰忎竴涓媝aram.get("username")鏄竴涓猄tring鏁扮粍(涓轟粈涔堣榪斿洖鏁版嵁鎴戜篃涓嶇煡閬擄紝鎴戜粠weblogic紿楀彛鐪嬪埌param.get("username")琚玱ut鍑烘潵Ljava.lang.String錛屽繖媧諱簡鍗婂ぉ)
String value[] = (String[])param.get("username");
String username = "";
for(int i=0;i<value.length;i++)
{
username +=value[i];
}
榪欐牱灝卞彲浠ュ緱鍒版紜殑username浜?/P>
絎簩縐嶆柟娉曟槸鐩存帴鎶妑equest寮曠敤榪涙潵
ServletActionContext.getRequest().getParameter("username")
ServletActionContext.getRequest()灝辨槸httpservletrequest
榪欎釜綾誨啀import com.opensymphony.webwork.ServletActionContext
鐢ㄨ搗鏉ユ柟渚夸簺
鍒氬垰鎺ヨЕ浜咶reeMarker瑙夊緱涓嶉敊
鍋氫簡涓皬渚嬪瓙
鍒氬垰鎺ヨЕ浜咶reeMarker瑙夊緱涓嶉敊鍋氫簡涓皬渚嬪瓙,娌℃湁鐢ㄥ埌jsp鍜宩sptag
棣栧厛寤虹珛涓暟鎹〃閲岄潰鏈塽sername,password涓や釜瀛楁鑷繁闅忎究鍔犲嚑鏉℃暟鎹?BR>寤虹珛涓涓獁ebwork action TestAction.java
鍦▁work.xml閲屽姞鍏ュ涓嬪唴瀹?BR>----------------------------------------
----------------------------------------
TestAction.java浠g爜濡備笅
--------------------------------------------------
package com.action;
import com.opensymphony.xwork.ActionSupport;
import java.util.*;
import com.ResultGather;
public class TestAction extends ActionSupport
{
private List lis;
private ResultGather rs;
public List getLis()
{
return this.lis;
}
public String execute() throws Exception
{
rs = new ResultGather();
lis = rs.selectRS("select * from userbasedatum");//鏁版嵁緇撴灉鎴戞槸鐢↙ist,HashMap灝佽鐨?HashMap瀛樼殑鏄崟鏉¤褰?BR> return SUCCESS;
}
}
-------------------------------------------------------------------------------------------------------------------
ResultGather.java浠g爜濡備笅
----------------------------------------------------------
package com;
import conn.DBConnManager;//鏁版嵁搴撹繛鎺ユ睜
import java.sql.*;
import java.util.*;
public class ResultGather
{
private String sql;
public ResultGather()
{
}
public ResultGather(String sqlcom)
{
this.sql=sqlcom;
}
public List selectRS(String sqlStr)
{
this.sql = sqlStr;
return selectRS();
}
public List selectRS() //鏁版嵁緇撴灉鍦ㄨ繖閲岃鍏ist鍜孧ap
{
List rsall = new ArrayList();
DBConnManager conn = null;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
Map rsTree;
try{
conn = DBConnManager.getInstance();
con = conn.getConnection("mssql");
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
while(rs.next())
{
rsTree = new HashMap(numberOfColumns);
for(int r=1;r
rsTree.put(rsmd.getColumnName(r),rs.getObject(r));
}
rsall.add(rsTree);
}
}catch(java.lang.Exception ex){
ex.printStackTrace();
}finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.releaseConnection("mssql",con);
}catch(Exception e){
System.out.println(e);
}
}
return rsall;
}
}
-----------------------------------------------------------------------------------------------
test.flt浠g爜濡備笅
-----------------------------------------------------------------------------------------
<#list lis as x>
${x.username},
${x.password}
</#list>
#LIST> //freemarker鐢ㄦ硶緗戜笂鏈夌浉鍏充粙緇?BR>-------------------------------------------------------------------------------------
OK 瀹屾垚浜?娌℃湁鐢ㄥ埌jsp<%.....%>鍜宩sptag
鐢ㄨ繖涓緥瀛愬仛涓鐐逛慨鏀瑰氨鍙互鍏鋒湁鍒嗛〉鐢ㄨ兘浜?/P>