1

I have a table inside a form that contains dropdowns and a hidden input.

JSP:

<form action="modifyfaculty" method="post">
    <table>
        <thead class="center">
            <tr>
                <!-- column names here -->
            </tr>
        </thead>

        <tbody>
            <c:forEach var="professor" items="${facultyList}">
                <tr>
                    <!-- more <td> -->

                    <td align="center">
                        <c:choose>
                            <c:when test="${professor.profEmplStatus.equals('FULL')}">
                                <select name="profEmplStatus" required>
                                    <option value="FULL" selected>FULL</option>
                                    <option value="PART">PART</option>
                                    <option value="RET">RET</option>
                                    <option value="TRMTD">TRMTD</option>
                                </select>
                            </c:when>

                            <!-- more <c:when> blocks -->
                        </c:choose>
                    </td>

                    <td align="center">
                        <c:choose>
                            <c:when test="${professor.profDept.equals('BSCS-SE')}">
                                <select name="profDept" required>
                                    <option value="BA-MMA">BA-MMA</option>
                                    <option value="BFDT">BFDT</option>
                                    <option value="BS-AN">BS-AN</option>
                                    <option value="BS-GPD">BS-GPD</option>
                                    <option value="BSBA-FM">BSBA-FM</option>
                                    <option value="BSBA-MKT">BSBA-MKT</option>
                                    <option value="BSCS-SE" selected>BSCS-SE</option>
                                    <option value="BSIT-WD">BSIT-WD</option>
                                    <option value="GENED">GENED</option>
                                </select>
                            </c:when>

                            <!-- more <c:when> blocks -->
                        </c:choose>
                    </td>

                    <td class="center">
                        <input type="hidden" name="profId" value="${professor.profId}" />
                        <input type="submit" value="Save" />
                    </td>
                </tr>
            </c:forEach>
        </tbody>
    </table>
</form>

ModifyFacultyAction:

public class ModifyFacultyAction extends ActionSupport {
    private static final long serialVersionUID = 1L;

    private Integer profId;
    private String profEmplStatus;
    private String profDept;

    @Override
    public String execute() {
        FacultyManager fm = new FacultyManager();
        fm.modifyFaculty("professor_employment_status", profEmplStatus, profId);
        fm.modifyFaculty("professor_department", profDept, profId);

        return SUCCESS;
    }

    public Integer getProfId() {
        return profId;
    }

    public void setProfId(Integer profId) {
        this.profId = profId;
    }

    public String getProfEmplStatus() {
        return profEmplStatus;
    }

    public void setProfEmplStatus(String profEmplStatus) {
        this.profEmplStatus = profEmplStatus;
    }

    public String getProfDept() {
        return profDept;
    }

    public void setProfDept(String profDept) {
        this.profDept = profDept;
    }
}

struts.xml:

<action name="modifyfaculty" class="com.mypackage.action.ModifyFacultyAction">
    <interceptor-ref name="notauth" />
    <interceptor-ref name="defaultStack" />

    <result type="redirectAction">
        <param name="actionName">viewfaculty</param>
    </result>

    <result name="input" type="redirectAction">
        <param name="actionName">viewfaculty</param>
    </result>

    <result name="index" type="redirect">/index.jsp</result>
</action>

Output: enter image description here

However, upon choosing a new value from the dropdown and clicking Save, I am receiving an error. Below is the devMode log:

Jan 05, 2015 11:34:43 PM com.opensymphony.xwork2.interceptor.ParametersInterceptor error
SEVERE: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'profId' on 'class com.mypackage.action.ModifyFacultyAction: Error setting expression 'profId' with value ['100005', '100006', '100007', '100008', '100009', '100010', '100011', ]
Jan 05, 2015 11:34:43 PM com.opensymphony.xwork2.util.LocalizedTextUtil warn
WARNING: Missing key [invalid.fieldvalue.profId] in bundles [[org/apache/struts2/struts-messages, com/opensymphony/xwork2/xwork-messages]]!

How to resolve this?

k_rollo
  • 5,304
  • 16
  • 63
  • 95
  • Add `input` result and some tags (`actionerror`/`fielderror`) to jsp to see error(s). – Aleksandr M Jan 05 '15 at 11:58
  • I added the ``, but it must redirect to another action (`type="redirectAction"`) to render the needed JSP, so I cannot see the field error messages. – k_rollo Jan 05 '15 at 12:36
  • 1
    So don't redirect. Turn the dev mode on. – Aleksandr M Jan 05 '15 at 13:24
  • At the very least, I have narrowed down the cause to the form being submitted 7 times (the number of professors/database entries) when clicking `Save`, since the entire table is inside the form. I am still unsure of how to achieve the intended goal. – k_rollo Jan 06 '15 at 01:33
  • Do you want to save whole table at once? If not then create separate forms or whatever. – Aleksandr M Jan 06 '15 at 09:27
  • "create separate forms or whatever" - How to do that is what I'm hoping for as an answer, since it is illegal to put a `` in a `
    `.
    – k_rollo Jan 07 '15 at 08:37
  • So basically your question is: how to put form inside table? Read the accepted answer of this question you've linked. – Aleksandr M Jan 07 '15 at 08:42
  • The accepted answer does not have the example. A comment was asking for an example (upvoted 5 times, including mine). I have also commented to notify the poster. – k_rollo Jan 07 '15 at 08:46

1 Answers1

1

You don't need to submit all ids of the table. Because you don't want to convert profId to a list or array. Use a separate form for each record.

<table>
    <thead class="center">
        <tr>
            <!-- column names here -->
        </tr>
    </thead>

    <tbody>
        <c:forEach var="professor" items="${facultyList}">
          <tr><td>
          <form action="modifyfaculty" method="post">
          <table>
            <tr>
                <!-- more <td> -->

                <td align="center">
                    <c:choose>
                        <c:when test="${professor.profEmplStatus.equals('FULL')}">
                            <select name="profEmplStatus" required>
                                <option value="FULL" selected>FULL</option>
                                <option value="PART">PART</option>
                                <option value="RET">RET</option>
                                <option value="TRMTD">TRMTD</option>
                            </select>
                        </c:when>

                        <!-- more <c:when> blocks -->
                    </c:choose>
                </td>

                <td align="center">
                    <c:choose>
                        <c:when test="${professor.profDept.equals('BSCS-SE')}">
                            <select name="profDept" required>
                                <option value="BA-MMA">BA-MMA</option>
                                <option value="BFDT">BFDT</option>
                                <option value="BS-AN">BS-AN</option>
                                <option value="BS-GPD">BS-GPD</option>
                                <option value="BSBA-FM">BSBA-FM</option>
                                <option value="BSBA-MKT">BSBA-MKT</option>
                                <option value="BSCS-SE" selected>BSCS-SE</option>
                                <option value="BSIT-WD">BSIT-WD</option>
                                <option value="GENED">GENED</option>
                            </select>
                        </c:when>

                        <!-- more <c:when> blocks -->
                    </c:choose>
                </td>

                <td class="center">
                    <input type="hidden" name="profId" value="${professor.profId}" />
                    <input type="submit" value="Save" />
                </td>
            </tr>
           </table>
          </form>
         </td></tr>
        </c:forEach>
    </tbody>
</table>
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Hi, from [this thread](http://stackoverflow.com/questions/5967564/form-inside-a-table), it says you cannot have a `
    ` inside ``?
    – k_rollo Jan 07 '15 at 08:34
  • If you can include an example to Quentin's solution, I would be glad to accept the answer here and link that thread to this post (since it seems to be a popular question). =) – k_rollo Jan 07 '15 at 09:54
  • Use form inside a cell, then you need an internal table for the columns of the outer table. See updated code. – Roman C Jan 07 '15 at 10:13
  • Hey, the nested tables worked, thanks! I upvoted it for working as it should. The tables just appeared messed up though ([screenshot](http://s11.postimg.org/p01u14fbn/tables.png)). Here is the updated code ([pastebin](http://pastebin.com/iZR5P98D)). How do I make it so it appears like the Output image in my original post? – k_rollo Jan 08 '15 at 10:15
  • 1
    It's different question, more likely to page design, please ask in another post. You can make link to this question of course. And you should know that if this answer worked for you should mark it as accepted answer. – Roman C Jan 08 '15 at 10:24
  • All right, thanks. Figured it might be more appropriate as an HTML question. Marking as accepted as well. Will post another thread for the table formatting. I will also link this to Quentin's answer. – k_rollo Jan 08 '15 at 10:28
  • 1
    This is going to end up with tables that don't describe the actual data structure and which don't line up with each other. Better to use that approach I described in the linked to question. – Quentin Jan 08 '15 at 10:36
  • 1
    Hi @Quentin, yes it had misaligned cells as you can see in my screenshot a few comments above (though I'm keeping this as the accepted answer, since it answered the original question regarding functionality). If you could put an example code snippet in your original answer in the other thread, that would be much helpful. – k_rollo Jan 08 '15 at 10:40
  • I don't see how an example would help at all. The two sentences explain it much more clearly than a couple of dozen lines of HTML and whatever server side language I feel like writing for the example would. – Quentin Jan 08 '15 at 10:46
  • @Quentin, it would really help as there are requests for it. No need to show the server side, really. Just the HTML. – k_rollo Jan 08 '15 at 10:59
  • The HTML would be a couple of submit buttons. There's nothing complicated enough about it to benefit from an example. – Quentin Jan 08 '15 at 11:01