2012-02-13 30 views
1

これは繰り返しの質問かもしれませんが、お詫び申し上げます。私はいくつかのボタンがあるjspページを持っています。各ボタンには、呼び出す独自のサーブレットがあります。私は、ユーザが与えられた3つの機能のいずれかを選択できるので、フォームを使わずにこれらのサーブレットを呼び出せる方法があるかどうかを知りたいと思います。 JSPページから私が呼び出すサーブレットに値を渡す必要もあります。フォームを使わずにjspからサーブレットを呼び出す方法

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Configurations</title> 
<script type="text/javascript"> 
    function runConfiguration(){ 
     var config=${dataValues.get(0)}; 
     //call servlet 
    } 
    function editConfiguration(){ 
     var config=${dataValues.get(0)}; 
     //call servlet 
    } 
    function deleteConfiguration(){ 
     var config=${dataValues.get(0)}; 
     //call servlet 
    } 
</script> 
</head> 
<body> 
<% 
String[] label={"Master Port","Baud Rate","Char Size","Stop Bits","Parity","RTU Port","Baud Rate","Char Size","Stop Bits", "Parity"}; 
int i=0; 
%> 
<br> 
<br> 
<br> 
<table align="center" border="1"> 
    <td><div align="center" style="background-color: goldenrod;"><b> ${dataValues.get(0)}</b></div> 
     <table width="210" align="left" border="1"> 
      <td bgcolor="goldenrod"><b> Header1 </b></td> 
      <c:forEach var="data" begin="1" end="5" items="${dataValues}" varStatus="status"> 
      <tr> 
       <td><%=label[i++]%>: ${data}</td> 
      </tr> 
      </c:forEach> 
     </table>  
     <table width="210" align="left" border="1"> 
      <td bgcolor="goldenrod"><b> Header2 </b></td> 
      <c:forEach var="data" begin="6" end="10" items="${dataValues}" varStatus="status"> 
      <tr> 
       <td><%=label[i++]%>: ${data}</td> 
      </tr> 
     </c:forEach> 
     </table> 
    </td> 
</table> 
     <c:choose> 
     <c:when test="${dataValues.get(11)==1}"> 
       <p align="center"><b><i>This configuration is already running</i></b></p> 
       <p align="center"> 
      <input type="button" value="stop" onclick="StopConfiguration"/> 
       </p> 
     </c:when> 
     <c:otherwise> 
       <p align="center"><b><i>This configuration is currently NOT running</i></b></p> 
       <p align="center">      
        <button type="button" onclick="runConfiguration()">Run</button> 
        <button type="button" onclick="editConfiguration()">Edit</button> 
        <button type="button" onclick="deleteConfiguration()">Delete</button> 
       </p> 
     </c:otherwise> 
    </c:choose> 
</body> 
</html> 
+0

+1にお役立ちし、有益な –

答えて

3

私はここに2つのオプションを参照してください。

  1. は、右のPOSTリクエストをsendiing前に、JavaScriptを使用して、各ボタンに応じて、フォームのURLを変更します。

  2. 3つのケースすべてでフォームと同じサーブレットを使用します。サーブレットでは、どのボタンが押されたのか(それらの値は要求パラメータとして渡されます)を決定し、それに応じて前進します。

+0

詳細については、これを見.....一例であり、/ajax/http-form-post-request-using-ajax-and-servlet – MJM

+0

+1の2番目のオプションです。より構造化されている – Jayy

0

あなたはこのまたはより悪いが、実行可能な方法を行うためのAjaxを使用することができ、あなたは現在のウィンドウのURLを変更し、ページをリロードすることができます。

Ajaxコールでは、ExtJSを使用することをおすすめします。他の方法についてExt.Ajax Usage

を参照してください:change the hrefreload the window

0

サーブレットにおけるそれのためのあなたのJSP &チェックでアクション文字列を指定してください。

その文字列アクションに応じて、サーブレットコード内の条件を設定して、その特定のアクションが実行するアクションを設定できます。 これは私のコードで行ったことです以下を参照してください。代わりに、多くのサーブレットを呼び出すので

例えば

<A href="<%=request.getContextPath()%>/JobAction?action=runConfig">RUN</a> 

<a href="<%=request.getContextPath()%>/JobAction?action=editConfig">EDIT</a> 

、あなただけの1つのサーブレットを呼び出すことができます。 このようなサーブレットのコードを使用します。あなたはまた、Javascriptを使用して、ユーザーがボタンのスクリプトを呼び出して、動的にURLを変更し、そのフォームを送信を打つ時にでき

String action = request.getParameter("action"); 
if (action.equalsIgnoreCase("runConfig")) { 
    // Specify what you want to do 
} else if (action.equalsIgnoreCase("editConfig")) { 
    // Specify what you want to do 
} else if (action.equalsIgnoreCase("deleteConfig")) { 
    // Specify what you want to do 
} 
0

。 http://www.hiteshagrawal.comを:ここ

<form action = "something".....> 
    your stuff here.... 
    <button type="button" onclick="runConfiguration(actionName)">Run</button> 
    <button type="button" onclick="editConfiguration(actionName)">Edit</button> 
    <button type = "button" onclick = "deleteConfiguration(actionName)">Delete</button> 

そしてスクリプト

<script type="text/javascript"> 
     function runConfiguration(actionName){ 
      var config=${dataValues.get(0)}; 
      goToPage(actionName); 
      //call servlet 
     } 
     function editConfiguration(actionName){ 
      var config=${dataValues.get(0)}; 
      goToPage(actionName) 
      //call servlet 
     } 
     function deleteConfiguration(actionName){ 
      var config=${dataValues.get(0)}; 
      goToPage(actionName) 
      //call servlet 
     } 

function gotopage(actionname) 
{ 
     document.formName=actionname; 
     document.formName.submit(); 
} 
    </script> 
関連する問題