I am new to jsp in my jsp i have
href="${mainUrl}/userdata?page=1" target="_blank">Next On First Page
href="${mainUrl}/userdata?page=1" target="_blank">Next On 2nd Page
href="${mainUrl}/userdata?page=1" target="_blank">Next On 3rd Page
href="${mainUrl}/userdata?page=1" target="_blank">Next On 4rth Page
However , i want this link on every page
Requirement :
Next
So i applied for-each loop on tag
<c:forEach items="${i}" var="i">
Next
</c:forEach>
${i} : Its an attribute from my controller with int i=0;
However , compiler is saying it cant interate on "i"
How can i fix that? I am doing Pagination
Assuming userdata is your controller. Make the controller set the next page's id, not the current one's, and name it something better than i, like nextPageID.
So in the controller, take the current id received in the request parameter page, if any, and add one to it, and set it in the request attribute nextPageID. I guess if no page id was received, the next id would 2. Then in your JSP:
Next
Because if you are adding one in the JSP, how do you know this isn't the last page? Keep the logic part in the controller. The controller will presumably know which number is the last page.
BTW, you obviously cannot iterate on a simple int; Iteration means traversing over an array or list.
Related
I am working on a test that checks that all images on a page are visible. I'm running into an issue where its only pulling the link from the first img on the page and logs it the length of the loop. Im currently getting a count of all the images, and in that count I loop through and pull the img source. There are no special classes, or ids. The only thing I have to go off of is . I'm guessing I will somehow need to parse the entire HTML since robotframework only looks at what is viewable on the screen?
My end goal is to pull all img sources on a page and confirm each one returns a 200 status code.
Here is what I have now:
#{all_image_sources} Create List
${all_images} Get Element Count //body//img
FOR ${image} IN RANGE ${all_images}
${img_src} Get Element Attribute tag:img src
log ${img_src}
Append To List ${all_image_sources} ${img_src}
END
Log List ${all_image_sources}'''
You might consider using Get WebElements, this will give you each image locator in a list. You can then loop through the list to get each src attribute.
example:
#{all_image_sources} Create List
${all_images} Get WebElements //body//img
FOR ${image} IN #{all_images}
${img_src} Get Element Attribute ${image} src
Append To List ${all_image_sources} ${img_src}
END
Log List ${all_image_sources}
Get WebElements
I know I'm not supposed to use the Viewbag and that I should build a menu using Html.DropDowlListFor() so that i can add attribute bound to the members of the model, BUT, that would involve a pretty extensive code rewrite....
I have a custom controller with a menu:
*.ASCX
<%: Html.DropDownList("CityIDs", new SelectList(ViewBag.cities, "Id", "Name"), "--Select--", new { style = "width:200px" })%>
<%= Html.ValidationMessage("CityIDs") %>
The List populates just fine and I can default to the top item to "--Select--"
The prbolem is that I want the validation error to occur on anything that is not from the viewbag.... how can I achieve this?
Validation for dropdown lists only ensures that something was posted. If you want to ensure that the value that was posted is actually one of a set of "allowed" values, then you'll have to manually do that in your post action:
var cityIds = db.Cities.Select(m => m.Id);
if (!cityIds.Contains(model.CityIDs))
{
ModelState.AddModelError("CityIDs", "You must select one of the available choices.");
}
if (ModelState.IsValid)
{
...
Two things:
Notice that I'm pulling the city ids straight from the database (the actual code you'd need here, of course, depends on your specific implementation). The important thing, though, is that ViewBag only survives a single request, so you can't look for them in ViewBag after posting.
Despite the pluralized name of CityIDs, using the DropDownList helper ensures that only a single selected value will exist. If this is actually supposed to be a multiselect, then you need to use ListBox instead, and update this conditional here to account for check for multiple values.
Populates the top item with Text = "--Select--" and Value = ""
which will post a blank value for CityIDs and cause an error in ModelState.
Like other 'List' page, I'm making a <a> tag on td, and 3buttons 'Go up','Go down','Go list'.
I succeed after click this link, go another page.
<%#Eval("Title")%>
Now, the problem is here. To make Go up/down pages, I guess put the number +1 on the link to calculate page number.
<%#Eval("Title")%>
Then, when I check address, result just show +1 not calculate.
//address : http://localhost:61375/NoticeDetail.aspx?seq=+1
How can I calculate on Eval Databinding data and show that? or, do you know how to make a function up/down page with Eval?
If you have a string value in the database and you expect it to always be an integer and it shouldn't be null, then you should use int.Parse()
<%#Eval("Title")%>
Otherwise if you don't know the type of data and you don't sure about the null values then should use Convert.ToInt32(). Convert.ToInt32() returns 0 for null, while int.Parse() throws a ArgumentNullException
<%#Eval("Title")%>
You're not parsing the int, try this (if your int is nullable though, make sure you check for that first):
<a href='NoticeDetail.aspx?seq=<%# int.Parse(Eval("Seq").ToString()) + 1 %>'><%#Eval("Title")%></a>
I'm building a simple forum where the SQL query changes whats pulled out by what link is clicked and what I thought I could do was just set a varaible to the value of the topic name and it would give each row their own variable value, but it overwrites it everytime it loops through the forEach and only ever pulls out data for the last row it outputs, but with the id of the <a> it sets the value to the right row in the table.
Is there anyway I can set the variable to the value of the <a> id or would I have to populate an array full and set the varaible to the value of the next element of the array
<td><a id="${row.topic_name}" href="ViewPosts.jsp" onclick='<c:set var="post" value="" scope="application"></c:set>'><c:out value="${row.topic_name}"></c:out></a></td>
A bit of a roundabout solution didn't take too much code though, used javascript to get the id passed it to a hidden input and got the javascript to auto submit to a jsp file that forwarded the parameter I needed onto the jsp page that called the query.
Hi I am using s:url tag for making a url, I need it to paginate my jsp to several pages.
So I need to send next or previous when we click on next or previous link. But when I click on Previous it shows me url on address bar like someAction?previuos=prev then I click on Next it displays someAction?previuos=prev&Next=Next when I only need someAction?Next=Next.
I have given different names to the url as for Previous link <s:url name="urlPrev"> and for next <s:url name="urlNext">
I don't how to differentiate them. What is wrong in my code?
The <s:url/> tag should default to the current action, without any parameters. You should just be able to say in your page:
<s:url>
<s:param name="Next" value="'Next'"/>
</s:url>
<s:url>
<s:param name="Previous" value="'Previous'"/>
</s:url>
Note that the <s:a/> tag behaves much like the tag excepting that it produces an html anchor of course so put the link name in the body. This should solve your tag issues...
Just to point out paging is roughly done on web sites by passing parameters which account for start_index and number_of_records_per_page. The "Next" button then simply adds number_of_records_per_page + 1 for the value of start_index for next. To decrement subtract number_of_records_per_page. By standardising the names all list based actions become easier to write.
Consult http://struts.apache.org/2.3.1.2/docs/tag-reference.html for use of these tags (one thing to note is the name parameter is not listed as a property for the url tag).