Source

webwork / docs / multiplesubmit.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <title>WebWork Documentation</title>
  <link type="text/css" href="main.css" rel="STYLESHEET"/>
</head>
<body>
  <div id="page-logo">
    <a href="index.html"><img src="logo-small.png" border="0"/></a>
  </div>
    <div class="snip-title">
	  <h1 class="snip-name">Multiple Submit Buttons
  
  </h1>
  </div>
<div id="snip-content" class="snip-content">

 <div class="snip-attachments"></div>
 
 <h3 class="heading-1">Introduction
</h3>
Often, we have multiple submit buttons within a single form. The below is just a simple way of identifying which button was clicked, and which actions to take.<p class="paragraph"/>There are, of course, many ways of doing this, including the use of javascript to identify the actions, etc&#8230; You're welcome to pick and choose whichever method you find most useful. Webwork is flexible enough.
<h3 class="heading-1">Form
</h3>
<div class="code"><pre>&#60;button type=<span class="java&#45;quote">"submit"</span> value=<span class="java&#45;quote">"Submit"</span> name=<span class="java&#45;quote">"submit"</span>&#62;
&#60;button type=<span class="java&#45;quote">"submit"</span> value=<span class="java&#45;quote">"Clear"</span> name=<span class="java&#45;quote">"clear"</span>&#62;</pre></div>
<h3 class="heading-1">Action with boolean properties
</h3>
<div class="code"><pre>class MyAction <span class="java&#45;keyword">extends</span> ActionSupport &#123;
   <span class="java&#45;keyword">private</span> <span class="java&#45;object">boolean</span> submit;
   <span class="java&#45;keyword">private</span> <span class="java&#45;object">boolean</span> clear;
   <span class="java&#45;keyword">public</span> void setSubmit(<span class="java&#45;object">boolean</span> submit) &#123;
      <span class="java&#45;keyword">this</span>.submit = submit;
   &#125;
   <span class="java&#45;keyword">public</span> void setClear(<span class="java&#45;object">boolean</span> clear) &#123;
      <span class="java&#45;keyword">this</span>.clear = clear;
   &#125;
   <span class="java&#45;keyword">public</span> <span class="java&#45;object">String</span> execute() &#123;
      <span class="java&#45;keyword">if</span> (submit) &#123;
         doSubmit();
         <span class="java&#45;keyword">return</span> <span class="java&#45;quote">"submitResult"</span>;
      &#125;
      <span class="java&#45;keyword">if</span> (clear) &#123;
         doClear();
         <span class="java&#45;keyword">return</span> <span class="java&#45;quote">"clearResult"</span>;
      &#125;
      <span class="java&#45;keyword">return</span> <span class="java&#45;keyword">super</span>.execute();
   &#125;
&#125;</pre></div>
<h3 class="heading-1">Explaination
</h3>
The boolean properties 'submit' and 'clear' will be set to 'true' or 'false' according weather the submit or clear form element is present in the submitted form.<p class="paragraph"/>In this case, the properties are boolean, therefore the values set would be boolean.<p class="paragraph"/>There is another method, using String properties, described below...
<h3 class="heading-1">Form
</h3>
<div class="code"><pre>&#60;button type=<span class="java&#45;quote">"submit"</span> value=<span class="java&#45;quote">"Submit"</span> name=<span class="java&#45;quote">"buttonName"</span>&#62;
&#60;button type=<span class="java&#45;quote">"submit"</span> value=<span class="java&#45;quote">"Clear"</span> name=<span class="java&#45;quote">"buttonName"</span>&#62;</pre></div>
<h3 class="heading-1">Action with String properties
</h3>
<div class="code"><pre>class MyAction <span class="java&#45;keyword">extends</span> ActionSupport &#123;
   <span class="java&#45;keyword">private</span> <span class="java&#45;object">String</span> buttonName;
   <span class="java&#45;keyword">public</span> void setButtonName(<span class="java&#45;object">String</span> buttonName) &#123;
      <span class="java&#45;keyword">this</span>.buttonName = buttonName;
   &#125;
   <span class="java&#45;keyword">public</span> <span class="java&#45;object">String</span> execute() &#123;
      <span class="java&#45;keyword">if</span> (<span class="java&#45;quote">"Submit"</span>.equals(buttonName)) &#123;
         doSubmit();
         <span class="java&#45;keyword">return</span> <span class="java&#45;quote">"submitResult"</span>;
      &#125;
      <span class="java&#45;keyword">if</span> (<span class="java&#45;quote">"Clear"</span>.equals(buttonName)) &#123;
         doClear();
         <span class="java&#45;keyword">return</span> <span class="java&#45;quote">"clearResult"</span>;
      &#125;
      <span class="java&#45;keyword">return</span> <span class="java&#45;keyword">super</span>.execute();
   &#125;
&#125;</pre></div>
<h3 class="heading-1">Explaination
</h3>
In this case, the properties are String, therefore the values set are also String in nature.<p class="paragraph"/>I don't really like this method, as it ties in the Action to the Form. (What happens if you want different text to show up on the button ? You would have to change both the form as well as the corresponding action.)
<h3 class="heading-1">Conclusion
</h3>
There are other ways to achieve the same functionality. There are pros and cons to each methods. Feedback welcome.<p class="paragraph"/>
  </div>
</body>
</html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.