hiding a row on the jsp in Struts 1.1 - java

Given the following,
<logic:notEqual name="ABCForm" property="externalFactor" value="true">
<tr>
<td width="20%" class="label" height="14"><bean:message key="factorDefault"/></td>
<td class="label">
<html:radio property="defaultState" value="Y"><bean:message key="Yes"/></html:radio>
<html:radio property="defaultState" value="N"><bean:message key="No"/></html:radio>
</td>
</tr>
</logic:notEqual>
How do I hide this row on the page at all times without deleting data associated with this row in the database?
I tried replacing logic:notEqual with logic:empty and the jsp is blank for that div. I have tried simply removing this entire block of code, and of course that works. I am just concerned if that affects the database, or if there any pages that rely on data associated with the code snippet.

Mr.gabybaby you do one thing...
in your database table you add another column like status
by default status value is active if any user wants to delete any record through your JSP just you change that record status=inactive..
you should display only active records in jsp...
i think this is the solution for your requirement

Related

Thymeleaf - conditional rendering based on value of object property

<tr th:each="student: ${students}">
<td th:text="${student.id}" />
<td th:text="${student.name}" />
</tr>
I have 10 students with different names. I only want to render the whole table if there is AT LEAST 1 student with student.name == "Felix". Otherwise I donĀ“t want to render the table.
How can I do that?
In the controller which returns this template, check the list in Java. If you find "Felix".equals(student.name) then add a variable like 'isRenderTable' to the context and do a conditional on the table in Thymeleaf, like:
<table th:if="${isRenderTable}">...</table>
If you don't want to touch the server side code, then you can use Thymeleaf and JavaScript. Render the student list into JS, then use JS to perform the logical comparison you want, then render the table via JS.
Another way to do it is to render the student list into JS, and also render the table in Thymeleaf (and keeping the table hidden with CSS), then in JS if you detect the proper condition, change the CSS to show the table.
Below code should do the trick:
<table th:if="${not students.?[name == 'Felix'].isEmpty()}">
<tr th:each="student: ${students}">
<td th:text="${student.id}" />
<td th:text="${student.name}" />
</tr>
</table>

How to handle buttons with same value but different names in Spring

I have a table that is dynamically created that has rows of movies.
It has a title column,
a media type column,
a rating column,
and a column that contains a "view" button.
When one of these view buttons is clicked,
I would like to go to a page that contains all the details of that movie by sending the title to the controller so I can query for it in mySql.
The problem is that for all these buttons,
the value is always going to be "view".
So my solution is to make the name of the button different as shown in the code below (this is the raw html generated from the jsp):
<html>
<body>
<form action="someAction">
<table>
<tr>
<th>title</th>
<th>type</th>
<th>rating</th>
</tr>
<tr>
<td>title1</td>
<td>DVD</td>
<td>R</td>
<td><input type="submit" name="mediaType.title1" value="view"></td>
</tr>
<tr>
<td>title2</td>
<td>DVD</td>
<td>R</td>
<td><input type="submit" name="mediaType.title2" value="view"></td>
</tr>
<tr>
<td>title3</td>
<td>BLU-RAY</td>
<td>PG-13</td>
<td><input type="submit" name="mediaType.title3" value="view"></td>
</tr>
</table>
</form>
</body>
</html>
I could then use a parameterMap to figure out which view was pressed.
That is messy and Spring has to have some way of being able to do this.
I thought something like this would work in the controller:
#RequestMapping("someAction", params = "mediaType.{title}=view")
public ModelAndView loadPage(String title) {
// use title to query mysql
// build Model
// return ModelAndView
}
However this doesn't work.
Is there something that I can use in Spring that would be simpler and cleaner like above instead of getting the parameterMap from the request?
If I'm understanding this correctly, you have rows of information organized into a table. You're looping through some kind of collection, which has the side effect of making each input field have similar 'name' attributes. As a result, it's difficult to determine which input fields you really care about. You've then chosen to use the button as an identifier (presumably, because only the one button you actually click on get's added to the request -all the others don't get submitted) to determine which 'row', and subsequent input fields.
I think this might all come down to which 'button' you're using. If you're using a literal input tag (type="submit" or "button") the 'value' attribute is what the user sees as the text on the button -so, you're forced to play shenanigans with the 'name' attribute (presumably by adding an index to the name, splitting the string once you get it out of the request, and using that identifier to get the other parameters out of the request that also have the same identifier appended to their 'name' attribute).
JSP
<input type="submit" name="view${varStatus.index}" value="View" />
HTML Source
<input type="submit" name="view3" value="View">
You should probably use the button tag instead. It allows the text that the user sees to be different than the value that is submitted in the request.
JSP
<button type="submit" name="view" value="${varStatus.index}" >View</button>
HTML Source
<button type="submit" name="view" value="3">View</button>
This gives you the 'identifier' for the row.
How Spring fits into this:
I played with the #RequestMapping and the #RequestParam annotations, and was unable to get Spring to give me the values directly into the controller. But, I was able to do it by writing a custom HandlerMethodArgumentResolver along with a custom annotation.
note: I'm using annotations and java config instead of xml config, AND probably more importantly -this was a quick and dirty example to get it working. adjust it as needed for your situation.
HandlerMethodArgumentResolver
Annotation
Controller method
Instead of putting the table inside one form, you could put multiple forms inside the table, in the TRs:
<tr>
<form action="someAction">
<td>title1<input type='hidden' name='title' value='title1' /></td>
<td>DVD</td>
<td>R</td>
<td><input type="submit" value="view"></td>
</form>
</tr>
Then each title has its own form and you send in the title (always with parameter name of title) from a hidden input.
The solution lies in this statement:
"I have a table that is dynamically created that has rows of movies".
Presumably,
you are looping through a list of movies and generating the table in the jsp file and each row has some unique identifier.
Add a hidden to identify the selected row.
Set the value of the hidden value in an onclick handler from the submit buttons.
Here is some example stuff:
<form ...>
<c:forEach blah var="row">
<tr>
...
<td><input type="submit" value="View" onclick="setSelectedRow('${row.id}')"/></td>
</c:forEach>
<input type="hidden" id="blammy" name="blammy" value=""/>
</form>
<script type="text/javascript">
function setSelectedRow(rowId)
{
... set blammy.value equal to rowId.
}
</script>
// the id is used by JavaScript to find the object in the DOM.
// the name is what appears in the request.
#RequestMapping("someAction")
public ModelAndView loadPage(
#RequestParameter("blammy") final String blammy)
{
...
}

Retrieving value of visible element using java

I have two elements with name price1 [BTW I know that having duplicate IDs is against standards, is this the same with NAME?]
<TR CLASS="Blocks" id="p_priceKILO" style="display: none ;">
<TD>Price:</TD>
<TD><INPUT TYPE="text" name="price1">$/kilo</TD>
</TR>
<TR CLASS="Blocks" id="p_pricePOUND" style="display: none ;">
<TD>Price:</TD>
<TD><INPUT TYPE="text" name="price1">$/pound</TD>
</TR>
Only one of these rows will be visible at one time (using javascript)
I use the following java code to retrieve price1
public PricePosition(HttpServletRequest request) {
this.price1=StringFunctions.StringToDouble(request
.getParameter("PRICE1"));
...
Is there any neat way to retrieve only the visible element?
I have a workaround - calling them price1a and price1b and retrieving the correct one based on my knowledge of which one is visible, but I wondered if there was another way.
You'll have to use JS again: when displaying a row, rename the inner corresponding input to displayedPrice for example, and get this parameter server-side.
When hiding a row, don't forget to rename it back.

Get Table Contents using HTML

<Table>
<tr>
<td>ID</td>
<td>NAME</td>
<td>MARKS</td>
<td>Grade</td>
<td><a href="AddRecord.jsp" >Update Record</a> </td>
</tr>
</Table>
How can I get table contents for modification, when I press Update hyperlink button? or how can I send these contents as a parameter to the page "AddRecord.asp".
If you are trying to submit a new row to a data table it sounds to me like you just need to use a traditional <form> and make the "Update Record" a submit type button. You are going to have to read up on using databases and JSP to process your update requests.
I suggest you start by reading on submitting forms at: http://www.w3schools.com/tags/att_form_action.asp
I think your best best is AJAX and JavaScript. Make an AJAX call to get the content and add it to the DOM using JavaScript.

Updating the value of a iterator tag in struts2

I have a jsp in which I have a drop down as
<s:select name="newQuestion.CertificationId" list="certificationList"
listKey="certificationId" listValue="certificationName"
headerKey="" headerValue="Select Certification"
label="Certification Name"
onchange="getQuestionsList(this.value)" />
When the dropdown value changes I can getQuestionsList. In the javascript function I submit to an action class where I modify the value of a questionList which is displayed in my JSP via an iterator.
The values of the questionList contain all questions and when I select a value from the above drop down I need to populate only those questions which belong to the id selected in the drop down. (I query the DB to load the questions in action class.)
Initially when the page is loaded I have all questions in questionList but after selecting a value from drop down I have the updated questions in the action class.
For displaying the values of question list I use a iterator tag
<div id="questionDetails" class="registrationDetails" style="display: none;">
<span><b>Question List</b></span>
<br>
<table class="registrationDetailsTable">
<tr class="tabledataheader">
<td>Question Id</td>
<td>Question Description</td>
</tr>
<s:iterator value="questionList">
<tr class="tabledatarow">
<td><s:property value="questionId" /></td>
<td><s:property value="questionDesc" /></td>
</tr>
</s:iterator>
</table>
</div>
The div is initially hidden and on select of a value in drop down I need to display the values of questionList which is taking old values as the page is not reloaded.
When I again come back to this jsp I am not seeing the new value as it is not getting updated.
Any heads up please
Why don't you try Struts2 Jquery Plugin. Here is the showcase with code.
What you need is
<sj:select ...> tag.
Let me know if you still need an example.
You would need to make few changes to the code. Let me list them down:
Move the following part in your initial JSP to another jsp, say questionList.jsp. The below part should be the only thing which should be inside the newly created jsp.
<span><b>Question List</b></span>
<br>
<table class="registrationDetailsTable">
<tr class="tabledataheader">
<td>Question Id</td>
<td>Question Description</td>
</tr>
<s:iterator value="questionList">
<tr class="tabledatarow">
<td><s:property value="questionId" /></td>
<td><s:property value="questionDesc" /></td>
</tr>
</s:iterator>
</table>
In your main page, you would need to replace the above code part with
<jsp:include page="questionList.jsp" flush="true"/>
Now deploy and see if everything is in place as before.
In your getQuestionsList() javascript function, make an ajax call to another action mapping say showQuestionList.action?certification=12. Here, 12 will be the id of the certification, which you will handle in the action. If you use certification as an action property, add a variable with the same name with getter and setter methods.
In your action method say showQuestionList(), retrieve the questionList and assign to the action variable.
On return of SUCCESS of the above method, the result should send only questionList.jsp. This will have the part to list just the necessary fields.
In the getQuestionsList() javascript function, after the ajax call being success, get the data and put it into the div with id questionDetails.
Show the div.

Categories