JSP File:
<s:form name="inquiryForm" method="post" theme="simple" > 
   Select roll no : 
   <s:select style="width: 200px;" 
      list="#{'1':'Student 01', '2':'Student 02', '3':'Student 03', '4':'Student 04'}"
      id="projectName" 
      name="studentRollNo" 
      readonly="false" 
      headerKey="-1" 
      headerValue="--- Select ---" 
   /> 
   <s:submit cssClass="page" action="academics" value="academics" /> 
   <s:submit cssClass="page" name="sports" value="sports" /> 
   <s:submit cssClass="page" name="library" value="library" /> 
</s:form> `
<package name="default" extends="struts-default">
    <action name="academics" class="ViewAcademics" > 
        <result name="success">pages/ViewAcademics.jsp</result> 
    </action> 
    <action name="sports" class="ViewSports" > 
        <result name="success">pages/ViewSports.jsp</result> 
    </action> 
    <action name="library" class="ViewLibrary" > 
        <result name="success">pages/ViewLibrary.jsp</result> 
    </action>
</package>----------------------------------------------------------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.
There are, of course, many ways of doing this, including the use of JavaScript to identify the actions, etc... You're welcome to pick and choose whichever method you find most useful. Struts 2 is flexible enough.
Form
| <button type="submit"value="Submit"name="submit"><button type="submit"value="Clear"name="clear"> | 
Action with boolean properties
| classMyAction extendsActionSupport {   privatebooleansubmit;   privatebooleanclear;   publicvoidsetSubmit(booleansubmit) {      this.submit = submit;   }   publicvoidsetClear(booleanclear) {      this.clear = clear;   }   publicString execute() {      if(submit) {         doSubmit();         return"submitResult";      }      if(clear) {         doClear();         return"clearResult";      }      returnsuper.execute();   }} | 
Explanation
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.In this case, the properties are boolean, therefore the values set would be boolean.
There is another method, using String properties, described below...
Form
| <button type="submit"value="Submit"name="buttonName"><button type="submit"value="Clear"name="buttonName"> | 
Action with String properties
| classMyAction extendsActionSupport {   privateString buttonName;   publicvoidsetButtonName(String buttonName) {      this.buttonName = buttonName;   }   publicString execute() {      if("Submit".equals(buttonName)) {         doSubmit();         return"submitResult";      }      if("Clear".equals(buttonName)) {         doClear();         return"clearResult";      }      returnsuper.execute();   }} | 
Explanation
In this case, the properties are String, therefore the values set are also String in nature.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.)
There are other ways to achieve the same functionality. There are pros and cons to each methods.
The more elegant solution is probably by using multiple mappings for same Action. This way you don't need to set "struts.enable.DynamicMethodInvocation" to "true".
In JSP
| <s:formmethod="post"action="mySubmitAction">    <s:submitvalue="Submit"/>    <s:submitvalue="Clear"action="myClearAction"/></form> | 
| <actionname="mySubmitAction"class="MyAction"method="submit">       <result>submit.jsp</result></action><actionname="myClearAction"class="MyAction"method="clear">       <result>submit.jsp</result></action> | 
| publicString submit() throwsException {    // submit button logic here    returnSUCCESS;}publicString clear() throwsException {    // clear button logic here    returnSUCCESS;} | 
For best practice, if you have common data loaded / managed by your actions (submit & clear), then for example, you can define a MyBaseAction class, extended by MySubmitAction and MyClearAction class. Then this is how they looks like:
In struts.xml
| <actionname="mySubmitAction"class="MySubmitAction">       <result>submit.jsp</result></action><actionname="myClearAction"class="MyClearAction">       <result>submit.jsp</result></action> | 
Then in the MyAction, MySubmitAction and MyClearAction class
MyAction.java
| publicclassMyAction extendsActionSupport {    // common data or logic here} | 
MySubmitAction.java
| publicclassMySubmitAction extendsMyAction {    publicString execute() throwsException {        // submit button logic here        returnSUCCESS;    }} | 
MyClearAction.java
public class MyClearAction extends MyAction {    public String execute() throws Exception {        // clear button logic here        return SUCCESS;    }}
  
 
No comments:
Post a Comment