DWR与Struts整合

阅读更多
1.下載DWR Version 2
https://dwr.dev.java.net/files/documents/2427/47504/dwr.jar

2.安裝DWR,把dwr.jar放到WEB-INF/lib下

web.xml中加入DWRServlet & ActionServlet
其中的部分要特別注意,ActionServlet要先初始化,所以數字要比較小.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30


       
action    
org.apache.struts.action.ActionServlet
    
      
config      
/WEB-INF/struts-config.xml    
    
      
debug      
2    
    
2  
  
    
action    
*.do  
  
    
dwr-invoker    
uk.ltd.getahead.dwr.DWRServlet    
      
debug      
true    
    
10  
  
   
 dwr-invoker    
/dwr/*  



dwr.xml中加入struts的設定,其中formBean的參數的value值,會對應到struts-config.xml中的設定

1
2
3
4
5
6
7
8


     
    
"struts"

 javascript="testFrm"

>      
"formBean"

 value="testActionForm"

/>    
  
  
  



struts-config.xml

1
2
3
4
5
6
7
8
9
10
11


     
    
"testActionForm"

 type="test.struts.testActionForm"

 />  
  
    
"testActionForm"

 path="/testAction"

 scope="session"

 type="test.struts.testAction"

 validate="false"

>      
"display"

 path="/display.jsp"

 />    
  
  
"ApplicationResources"

 />
 



testActionForm.java,getDate()會透過dwr,取得現在最新的日期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24


package



 test.struts; 
import



 org.apache.struts.action.*;
import



 java.util.*; 
public



 class



 testActionForm extends



 ActionForm {

     
private



 String strDate;     
public



 void



 setStrDate(String strDate) {

        
this.strDate = strDate;    
}

     
public



 String getStrDate() {

        
return



 strDate;    
}

    
 //dwr

    public



 String getDate() {

        
Date date = new



 Date();        
return



 date.toString();   
 }

 
}

testAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20


package



 test.struts; 
import



 org.apache.struts.action.ActionMapping;
import



 org.apache.struts.action.ActionForm;
import



 javax.servlet.http.HttpServletRequest;
import



 javax.servlet.http.HttpServletResponse;
import



 org.apache.struts.action.ActionForward;
import



 org.apache.struts.action.Action;
import



 org.apache.struts.action.*; 
public



 class



 testAction extends



 Action {

    
 public



 ActionForward execute(ActionMapping mapping, ActionForm form,                                 
HttpServletRequest request,                                 
HttpServletResponse response) {

         
testActionForm actionForm = (testActionForm) form;       
 System.out.println(actionForm.getStrDate());        
return



 mapping.findForward("display"

);    
}

}

date.jsp,在form的部分,請用struts 的 tag library,我把改成後,無法正常的接受到值.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38


<%@ page contentType="text/html; charset=Big5"

 %>
<%@ taglib uri="/WEB-INF/struts-bean.tld"

 prefix="bean"

 %>
<%@ taglib uri="/WEB-INF/struts-html.tld"

 prefix="html"

 %>
<%@ taglib uri="/WEB-INF/struts-logic.tld"

 prefix="logic"

 %>
 
title  
  
  
 
 
 
"testAction.do"

>
date:"strDate"

 size="30"

 > 
"button"

 onclick="refreshDate();"

 value="更新日期"

/>
   
送出   
 

display.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15


<%@ page contentType="text/html; charset=Big5"

 %>
<%@ taglib uri="/WEB-INF/struts-bean.tld"

 prefix="bean"

 %>
%@page import="test.struts.*"

%

 
 
test
"#ffffff"

>

您送出的日期:

"testActionForm"

 property="strDate"

/>
 
 

你可能感兴趣的:(DWR与Struts整合)