一個ExtJS+jsp+Servlet與資料庫連接且運行成功的例子

來源:互聯網
上載者:User

ExtJS雖然強大但是調試起來很困難。費盡周折但最終還是成功了。期間總結的技巧學習的知識我會在下一篇博文中加以總結。現在還是來粘代碼吧。

Servlet代碼(注意我命名為List,有關List的設定檔web.xml相關映射名也為List,所以一會兒在js裡通訊引用應為List)

    package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.StuDao;

import vo.StuVo;

public class List extends HttpServlet {

 
 public List() {
  super();
 }

 
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

  public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setHeader("content-type", "text/html; charset=gb2312") ;
  PrintWriter out = response.getWriter();
  StuVo stu=null;//相關bean,記錄學生資訊,有學號、姓名、性別、年齡。你需要自己完成編碼。
  StuDao dao=new StuDao();//資料庫操作方法集你需要自己完成編碼。
  ArrayList list=dao.selAll();//由dao調用selAll()方法可以查詢擷取資料庫表中所有學生資訊,你需要自己完成編碼。
  int count=list.size();
  out.print("{results:"+count+",rows:[");
  for(int i=0;i<count;i++){
   stu=(StuVo)list.get(i);
   out.print("{stuId:'"+stu.getStu_id()+"',stuName:'"+stu.getStu_name()+
     "',stuSex:'"+stu.getStu_sex()+"',stuAge:'"+stu.getStu_age()+"'");
   if(i==count-1){
    out.print("}");
   }
   else{
    out.print("},");
   }
  }
  out.print("]}");
//  out.print("{results:2,rows:[{userId:'1',userName:'1 ',userSex:'1',userAge:'1'}," +
//   "{userId:'2',userName:'2 ',userSex:'2',userAge:'2'}"+ 
//  這是便於觀察的原始句式。
//  "]}") ;
  
  out.flush();
  out.close();
 }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
             doGet(request,response);
}
 
 public void init() throws ServletException {
  // Put your code here
 }

}

JS代碼(當然你需要實現把ExtJS的組件添加到項目裡,其中 proxy:new Ext.data.HttpProxy({url:"List"})表示從List.java這個Servlet擷取資料)

 MyEditorPanel = Ext.extend(
 Ext.grid.EditorGridPanel,
 {
  
  constructor:function()
  {
   MyEditorPanel.superclass.constructor.call(
    this,
    {
     title:"學生資訊列表",
     width:415,
     autoHeight:true,
     border:true,
     collapsible:true,
     frame:true,
     
     loadMask:new Ext.LoadMask(
      //Ext.get("EditorPanel"),
      Ext.getBody(),
      {
       msg:"loading..."
      }
     ),
     enableHdMenu:false,
     footer:true,
    
     
     cm:new Ext.grid.ColumnModel([
      {header:"學號",dataIndex:"stuId"},
      {header:"姓名",dataIndex:"stuName"},
      {header:"性別",dataIndex:"stuSex"},
      {header:"年齡",dataIndex:"stuAge"}
     ]),
     ds:new Ext.data.Store({
      autoLoad:true,
      proxy:new Ext.data.HttpProxy({url:"List"}),
      reader:new Ext.data.JsonReader(
       {
        id:"stuId",
        totalproperty:"results",
        root:"rows"
       },
       Ext.data.Record.create([
        {name:"stuId"},
        {name:"stuName"},
        {name:"stuSex"},
        {name:"stuAge"}
       ])
      )
     })
    }
   ) ;
   
  }}
) ;

Jsp代碼(注意需要引入的ExtJS的相關標頭檔。)

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"><!--注意Myeclipse裡預設產生的jsp紅字部分是HTML 4.0,必須要改為XHTML 否則js顯示有問題-->
<html>
  <head>
    <base href="<%=basePath%>">
   
    <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="resources/css/ext-all.css" />
<script type="text/javascript" src="ext-base.js"></script>
<script type="text/javascript" src="ext-all.js"></script>
<script type="text/javascript" src="ext-lang-zh_CN-min.js"></script>
 <script type="text/javascript" src="list.js"> </script>
 <script>
     Ext.onReady(function(){
      
      var myEditorPanel = new MyEditorPanel() ;
      myEditorPanel.render("list") ;
     }) ;
    </script>
  
<title>學生資訊</title>
</head>

<body>
 

   <div id="list" style="height:150px; width:300px"></div>
  

</body>
</html>
代碼部分結束

雖然是零起點寫有關ExtJS的心得,但是這是寡人親身體會的寶貴經驗,相信會很有用。我把特別需要注意的地方都用紅字做了提醒,千萬注意。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.