Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

JSP does not call the right method in controller class

801863Dec 13 2011 — edited Dec 14 2011
Hi all,
I have a jsp file which loads a page at this address: http://localhost:8080/dir/list/

I've added two checkboxes to the page; user can check both checkboxes, only one or none. Following is code I have in jsp file:
<form class="checkboxForm">

<c:choose>																					
<c:when test='${newItems==true}'>
<input id="newItems" type="checkbox" value="#" name="newItems" class="checkbox2" checked="checked" /><strong>&nbsp;New Businesses&nbsp;&nbsp;&nbsp;</strong>													
</c:when>
<c:otherwise>
<input id="newItems" type="checkbox" value="#" name="newItems" class="checkbox2"/><strong>&nbsp;New Businesses&nbsp;&nbsp;&nbsp;</strong>
</c:otherwise>
</c:choose>

<c:choose>																					
<c:when test='${rejectedItems==true}'>
<input id="rejectedItems" type="checkbox" value="#" name="rejectedItems" class="checkbox3" checked="checked"/><strong>&nbsp;Rejected Businesses&nbsp;&nbsp;&nbsp;</strong>													
</c:when>
<c:otherwise>
<input id="rejectedItems" type="checkbox" value="#" name="rejectedItems" class="checkbox3"/><strong>&nbsp;Rejected Businesses&nbsp;&nbsp;&nbsp;</strong>
</c:otherwise>
</c:choose>

<a href="#" onclick="apply();">
<button name="apply" class="btn-primary" value="Apply" tabindex="4">Apply</button>
</a>
</form>

<script type="text/javascript">

function apply(){
	var newItems = document.getElementById("newItems").checked;
	var rejectedItems = document.getElementById("rejectedItems").checked;
	
	alert("Inside apply() method.");
	alert("newItems= " + newItems + ", rejectedItems: " + rejectedItems);	
			
	window.location = "<c:url value='/list/'/>" + newItems + "/" + rejectedItems;
	
	alert("window.location= " + window.location);
	alert("Add extra delay!");
	return false;
}
</script>
This is my Controller class:
// Method1: this method gets called when the user loads the page for the first time.

@RequestMapping(value="/list", method = RequestMethod.GET)
public String list(Model model, HttpServletRequest request) {		
		
		System.out.println("Controller: method1: Passing through...");
		
		model.addAttribute("newItems", Boolean.TRUE);
		model.addAttribute("rejectedItems", Boolean.TRUE);

		// Does other things.
	}

// Method3: this method gets called when user checks/unchecks one of the checkboxes and clicks on Apply button.

@RequestMapping(value="/list/{newItems}/{rejectedItems}", method = RequestMethod.GET)
public String list(@PathVariable Boolean newItems, @PathVariable Boolean rejectedItems, Model model, HttpServletRequest request) {		
				
		System.out.println("Controller: method3: Passing through...");
		
		model.addAttribute("newItems", newItems);
		model.addAttribute("rejectedItems", rejectedItems);

// Does other things.
			
	}
The way my jsp code is written now, it calls the correct method and displays the correct url. When page loads for the first time, it calls method1 and the url is:
http://localhost:8080/dir/list

When the user checks/unchecks one or both checkboxes and clicks on apply button, method3 is called and this url is displayed:

If both checkboxes are checked which both booleans are set to true:

http://localhost:8080/dir/list/true/true

Everything is fine... however, if I comment these two lines at the end of JavaScript apply() function,
alert("window.location= " + window.location);
alert("Add extra delay!");
Then method3 will never gets called; only method1 gets called and I see this url:
http://localhost:8080/url/list//?newItems=%23&rejectedItems=%23&apply=Apply

I do not know why it gets confused bet. method1 and method3 and cannot pick the right method?

I tried to use the POST method instead, but the page goes directly to our internal error page and it never reaches the method I wrote in my controller for POST:

I don't really know what I'm doing wrong? Any help is greatly appraciated.

Edited by: ronitt on Dec 13, 2011 2:35 PM

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jan 11 2012
Added on Dec 13 2011
3 comments
347 views