Eliethesaiyan
Eliethesaiyan

Reputation: 2322

Handling empty parameter from JSP view to a controller

I have a view that I want to use to display patient encounters selected from a range of two given date startDate an End date and display the result on the same page,however, I want to also when the page is loaded for the first time,to display all the result on that same page. As I am getting parameters from the request in my method,when the page is loaded for the first time,it throwing an empty params exception since the argument of those parameters ,startDate and End Date are empty when the page is loaded for the first time...here is my view

<%@ include file="/WEB-INF/template/include.jsp"%>
<%@ include file="/WEB-INF/template/header.jsp"%>

<%@ include file="template/localHeader.jsp"%>

<p>Hello ${user.systemId}!Add Interface Implementation Code Here</p>
<form action="/module/practicalexercise/manage" method="GET">
From Date:<openmrs_tag:dateField formFieldName="startDate" startValue=""/>

To Date:<openmrs_tag:dateField formFieldName="endDate" startValue="" />
<br/><input type="submit" value="Submit">


</form> 
<br/>
<b>Total :${resultCount}</b>
<br/>
<b>EncounterType Breakdown</b>
<table>
<tr>
<th>#</th><th>Encounter Type</th><th>Total</th></tr>
<c:forEach items="${EncTypeMap}" var="entry" varStatus="var">
 <tr><td><c:out value="${var.count}" /></td><td>${entry.key}</td> <td> ${entry.value}</td>   </tr>
</c:forEach>
</table>
<%@ include file="/WEB-INF/template/footer.jsp"%>

here is my controller

package org.openmrs.module.practicalexercise.web.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.EncounterType;
import org.openmrs.Encounter;
import org.openmrs.api.EncounterService;
import org.openmrs.api.context.Context;
import org.springframework.stereotype.Controller;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;



@Controller
public class PracticalExerciseManageController {

protected final Log log = LogFactory.getLog(getClass());

@RequestMapping(value = "/module/practicalexercise/manage", method = RequestMethod.GET)
public void manage(@RequestParam("startDate") String startDate,@RequestParam("endDate") String endDate,ModelMap model) {

    EncounterService encounterService = Context.getEncounterService();
    SimpleDateFormat format = new SimpleDateFormat("dd MM YYYY ");
    Date from=null;
    Date to=null;

    try {
           from=format.parse(startDate) ;

         to = format.parse(endDate);
    } catch (ParseException e) {

        e.printStackTrace();
    }
    List<Encounter> encounters = encounterService.getEncounters(null, null,
            from, to, null, null, null,true);
    List<EncounterType> encounterTypes = encounterService.getAllEncounterTypes();
    Map<String,Integer> countMap=new HashMap<String,Integer>();



         for(EncounterType encType:encounterTypes){
             int encTypeCount=0;
             for(Encounter encounter:encounters){
                 if(encounter.getEncounterType().getId().equals(encType.getId()))
                     encTypeCount++;

         }
            countMap.put(encType.getName(), encTypeCount);

     }

    model.addAttribute("user", Context.getAuthenticatedUser());
    model.addAttribute("EncTypeMap", countMap);
    model.addAttribute("size", countMap.size());
    model.addAttribute("resultCount",encounters.size());


}
}

Upvotes: 0

Views: 569

Answers (1)

cy3er
cy3er

Reputation: 1699

You can add required false to the dates, so it won't fail when they are not present:

@RequestParam(value = "startDate", required=false) String startDate

Upvotes: 1

Related Questions