gpt4 book ai didi

java - 处理从 JSP View 到 Controller 的空参数

转载 作者:行者123 更新时间:2023-12-01 13:53:51 25 4
gpt4 key购买 nike

我有一个 View ,我想用它来显示从两个给定日期开始日期和结束日期范围中选择的患者遭遇,并在同一页面上显示结果,但是,我还想在加载页面时显示第一次,在同一页面上显示所有结果。当我在我的方法中从请求中获取参数时,当第一次加载页面时,它会抛出一个空参数异常,因为这些参数的参数,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"%>

这是我的 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());


}
}

最佳答案

您可以向日期添加必需的 false,这样当日期不存在时也不会失败:

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

关于java - 处理从 JSP View 到 Controller 的空参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19754747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com