JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Web Framework » Struts  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 struts該如何避開驗證
ssmintle





發文: 26
積分: 0
於 2008-12-15 18:08 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
jsp如下
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<%@ page language="java" contentType="text/html; charset=BIG5"
  pageEncoding="BIG5"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="html" uri="/WEB-INF/lib/tlds/struts-html.tld"%>
<%@ taglib prefix="bean" uri="/WEB-INF/lib/tlds/struts-bean.tld"%>
 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>StrutsTest</title>
<script type="text/javascript">
 
function callMethod(callmethod){  
  document.getElementById("function").value=callmethod;
  document.form1.submit();
  
}
 
</script>
</head>
<body>
<html:errors/>
 
<form action="comein.do" method="post" name="form1">
<table border="1">
  <tr>
    <td>標題</td>
    <td>數值</td>
  </tr>
  <tr>
    <td>書籍名稱</td>
    <td><input name="bookname" type="text"></td>
  </tr>
  <tr>
    <td>isbn</td>
    <td><input name="isbn" type="text"></td>
  </tr>
  <tr>
    <td>價錢</td>
    <td><input name="price" type="text" ></td>
  </tr>
  
  <tr>
    <td colspan="2" align="center">
    <input type="button" value="submit"  onclick="callMethod('insert')">
    </td>
  </tr>
</table>
<input type="hidden" name="function" id="function">
</form>
<form action="comein.do">
<table border="1">
<tr>
<td colspan="2" align="center">
搜索
</td>
</tr>
<tr><td>
<input type="text" name="seaech">
</td><td>
<input type="hidden" name="function" value="search">
<input type="submit" value="查詢">
</td></tr>
 
</table>
 
</form>
 
<c:if test="${searchBook!=null}">
  <table border="1">
    <tr>
      <td>標題</td>
      <td>數值</td>
    </tr>
    <tr>
      <td>第幾筆</td>
      <td>${searchBook.id}</td>
    </tr>
    <tr>
      <td>書籍名稱</td>
      <td>${searchBook.bookname}</td>
    </tr>
    <tr>
      <td>isbn</td>
      <td>${searchBook.isbn}</td>
    </tr>
    <tr>
      <td>價錢</td>
      <td>${searchBook.price}</td>
    </tr>
    <tr>
      <td colspan="2" align="center">查詢成功</td>
    </tr>
  </table>
</c:if>
 
<c:if test="${booklist!=null}">
<table border="1">
    <tr>
      <td>id</td>
      <td>書籍名稱</td>
      <td>isbn</td>
      <td>價錢</td>
 
    </tr>
    <c:forEach items="${booklist}" var="bookDisplay">
      <tr>
        <td>${bookDisplay.id}</td>
        <td>${bookDisplay.bookname}</td>
        <td>${bookDisplay.isbn}</td>
        <td>${bookDisplay.price}</td>
      </tr>
    </c:forEach>
  </table>
 
</c:if>
<c:if test="${updateCount!=null}">
目前資料庫有${updateCount}筆資料。
</c:if>
</body>
</html>

執行不同動作值會藉由隱藏的欄位傳值判斷該執行啥動作
action如下
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package controller;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import model.Book;
 
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import service.iBookService;
 
public class HelloAction extends Action {
  static ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("test.xml");
  static iBookService ibs=(iBookService) ctx.getBean("UserServiceProxy");
  
  @Override
  public ActionForward execute(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    String function =request.getParameter("function");
 
    List<Book> booklist=new ArrayList<Book>();
    Book searchbook=new Book();
    int count=0;
    Book book=new Book();
    BeanUtils.copyProperties(book, form);
    if (function.equals("insert")){
      count=ibs.addBook(book);
      booklist=ibs.getAllBook();
      request.setAttribute("booklist", booklist);
      request.setAttribute("updateCount", count);
      return mapping.findForward("success");
    }else if(function.equals("search")){
      String searchstring =request.getParameter("search").trim();
      if(searchstring.equals("")){
      booklist=ibs.getAllBook();
      request.setAttribute("booklist", booklist);
      return mapping.findForward("success");
      }else{
        int tointsearchstring;
        try {
          tointsearchstring=Integer.parseInt("searchstring");
        } catch (Exception e) {
          return mapping.findForward("success");
        }        
        searchbook=ibs.getBookByBookID(tointsearchstring);
        request.setAttribute("searchBook", searchbook);
        return mapping.findForward("success");
      }
      
      
    }
    
    
    return mapping.findForward("success");
  }


只要執行搜索的時候,struts的驗證會去偵測我第一個form,就跳出欄位不能空白的訊息
,請問如果要避開該怎麼做呢?


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
1228 struts該如何避開驗證 ssmintle 4688 2008-12-15 18:08
1104 Re:struts該如何避開驗證 koji 112 2008-12-15 18:20
1186 Re:struts該如何避開驗證 ssmintle 50 2008-12-16 13:14
» JWorld@TW »  Web Framework » Struts

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8