0

我有一个视图,我想用它来显示从两个给定日期 startDate 和 End 日期的范围中选择的患者遭遇,并在同一页面上显示结果,但是,我还想在第一次加载页面时,在同一页面上显示所有结果。当我在我的方法中从请求中获取参数时,当第一次加载页面时,它会抛出一个空参数异常,因为这些参数的参数,startDate 和 End Date 在第一次加载页面时为空...这是我的看法

<%@ 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"%>

这是我的控制器

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());


}
}
4

1 回答 1

1

您可以将 required false 添加到日期,因此当它们不存在时它不会失败:

@RequestParam(value = "startDate", required=false) String startDate
于 2013-11-03T15:56:57.180 回答