Java 缺少url参数的jstl句柄情况
在spring mvc应用程序中,我遇到以下错误:Java 缺少url参数的jstl句柄情况,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,在spring mvc应用程序中,我遇到以下错误: HTTP Status 400 - Required String parameter 'ownerID' is not present 当我尝试加载以下url时: http://localhost:8080/petclinic/owners 它期待着 http://localhost:8080/petclinic/owners?ownerID=1 or some other valid number. 如何更改代码,使其在指定own
HTTP Status 400 - Required String parameter 'ownerID' is not present
当我尝试加载以下url时:
http://localhost:8080/petclinic/owners
它期待着
http://localhost:8080/petclinic/owners?ownerID=1 or some other valid number.
如何更改代码,使其在指定ownerID时加载预期内容,但在未指定ownerID或ownerID无效时也加载其他一些指定内容?这是我需要在jsp或控制器类的jstl中更改的内容吗
以下是OwnerController.java中处理/owners url模式的部分:
@RequestMapping(value = "/owners", method = RequestMethod.GET)
public String processFindForm(@RequestParam("ownerID") String ownerId, Owner owner, BindingResult result, Map<String, Object> model) {
Collection<Owner> results = this.clinicService.findOwnerByLastName("");
model.put("selections", results);
int ownrId = Integer.parseInt(ownerId);
Owner sel_owner = this.clinicService.findOwnerById(ownrId);//jim added this
model.put("sel_owner",sel_owner);
return "owners/ownersList";
}
@RequestMapping(value=“/owner”,method=RequestMethod.GET)
公共字符串processFindForm(@RequestParam(“ownerID”)字符串ownerID,所有者Owner,BindingResult,映射模型){
收集结果=this.clinicService.findOwnerByLastName(“”);
模型。放置(“选择”,结果);
int-ownrId=Integer.parseInt(ownerId);
Owner sel_Owner=this.clinicService.findOwnerById(ownrId);//jim添加了这个
模型出售(“卖方所有人”,卖方所有人);
返回“所有者/所有者列表”;
}
下面是ownersList.jsp的代码。请注意,它不接受我试图用于解决此问题的choose…when块:
<body>
<div class="container">
<table>
<tr>
<td></td>
<td>
<spring:url value="/owners/new" var="owner_newUrl"></spring:url>
<a href="${fn:escapeXml(owner_newUrl)}" class="btn btn-info" >Add New Owner</a>
</td>
</tr>
<tr>
<td width=160 valign="top">
<datatables:table id="owners" data="${selections}" cdn="true" row="owner" theme="bootstrap2"
cssClass="table table-striped" paginate="true" info="false"
cssStyle="width: 150px;" align="left" >
<datatables:column title="Name" cssStyle="width: 150px;" display="html">
<spring:url value="/owners?ownerID={ownerId}" var="ownerUrl">
<spring:param name="ownerId" value="${owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(ownerUrl)}"><c:out value="${owner.firstName} ${owner.lastName}"/></a>
</datatables:column>
</datatables:table>
</td>
<c:choose>
<c:when test="${!empty sel_owner}">
<td valign="top">
<table class="table table-striped" style="width:600px;">
<tr>
<td><b><c:out value="${sel_owner.firstName} ${sel_owner.lastName}"/></b></td>
<td></td>
</tr>
<tr>
<td><c:out value="${sel_owner.address}"/></td>
<td><c:out value="${sel_owner.city}"/></td>
</tr>
<tr>
<td><c:out value="${sel_owner.telephone}"/></td>
<td></td>
</tr>
<tr>
<td colspan=2>
<spring:url value="/owners/{ownerId}/edit" var="owner_editUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_editUrl)}" class="btn btn-info" >Edit This Owner</a>
<spring:url value="/owners/{ownerId}/pets/new" var="owner_newpetUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_newpetUrl)}" class="btn btn-info" >Add New Pet</a>
<spring:url value="/owners?ownerID={ownerId}&type=cats" var="owner_catsUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_catsUrl)}" class="btn btn-info" >Show Cats</a>
<spring:url value="/owners?ownerID={ownerId}&type=dogs" var="owner_dogsUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_dogsUrl)}" class="btn btn-info" >Show Dogs</a>
<spring:url value="/owners?ownerID={ownerId}&type=all" var="owner_allUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_allUrl)}" class="btn btn-info" >Show All Pets</a>
</td>
</tr>
<c:if test="${fn:endsWith(requestScope['javax.servlet.forward.query_string'], 'all')}">
<tr>
<td colspan=2>
<p>Pets</p>
<datatables:table id="pets" data="${sel_owner.pets}" cdn="true" row="pet" theme="bootstrap2"
cssClass="table table-striped" paginate="false" info="false" filter="false"
cssStyle="width: 350px;" align="left" >
<datatables:column title="Name" cssStyle="width: 200px;" display="html">
<c:out value="${pet.name}"/>
</datatables:column>
<datatables:column title="BirthDate" cssStyle="width: 300px;" display="html">
<joda:format value="${pet.birthDate}" pattern="yyyy-MM-dd"/>
</datatables:column>
<datatables:column title="Type" cssStyle="width: 200px;" display="html">
<c:out value="${pet.type.name}"/>
</datatables:column>
</datatables:table>
</td>
</tr>
</c:if>
<c:if test="${fn:endsWith(requestScope['javax.servlet.forward.query_string'], 'cats')}">
<tr>
<td colspan=2>
<p>Cats</p>
<datatables:table id="cats" data="${sel_owner.cats}" cdn="true" row="cat" theme="bootstrap2"
cssClass="table table-striped" paginate="false" info="false" filter="false"
cssStyle="width: 350px;" align="left" >
<datatables:column title="Name" cssStyle="width: 200px;" display="html">
<c:out value="${cat.name}"/>
</datatables:column>
<datatables:column title="BirthDate" cssStyle="width: 300px;" display="html">
<joda:format value="${cat.birthDate}" pattern="yyyy-MM-dd"/>
</datatables:column>
<datatables:column title="Type" cssStyle="width: 200px;" display="html">
<c:out value="${cat.type.name}"/>
</datatables:column>
</datatables:table>
</td>
</tr>
</c:if>
<c:if test="${fn:endsWith(requestScope['javax.servlet.forward.query_string'], 'dogs')}">
<tr>
<td colspan=2>
<p>Dogs</p>
<datatables:table id="dogs" data="${sel_owner.dogs}" cdn="true" row="dog" theme="bootstrap2"
cssClass="table table-striped" paginate="false" info="false" filter="false"
cssStyle="width: 350px;" align="left" >
<datatables:column title="Name" cssStyle="width: 200px;" display="html">
<c:out value="${dog.name}"/>
</datatables:column>
<datatables:column title="BirthDate" cssStyle="width: 300px;" display="html">
<joda:format value="${dog.birthDate}" pattern="yyyy-MM-dd"/>
</datatables:column>
<datatables:column title="Type" cssStyle="width: 200px;" display="html">
<c:out value="${dog.type.name}"/>
</datatables:column>
</datatables:table>
</td>
</tr>
</c:if>
</table>
</td>
</tr>
</c:when>
<c:otherwise>
Print this content if there is no ownerID.
</c:otherwise>
宠物
猫
狗
如果没有所有者ID,则打印此内容。
编辑: 根据Sotirios和Loc的建议,我将代码更改为以下内容,但仍然抛出相同的错误。是否有人可以明确地告诉我如何修复此问题,以便/owners url模式可以随所有者列表一起提供“一些文本”,而不是抛出错误或提供默认所有者的详细信息 以下是OwnerController.java中的相关方法:
public String processFindForm(@RequestParam(value="ownerID", required=false) String ownerId, Owner owner, BindingResult result, Map<String, Object> model) {
Collection<Owner> results = this.clinicService.findOwnerByLastName("");
model.put("selections", results);
boolean ownerIDValid;
if(ownerId==null){ownerIDValid = false;}
else{//I added this if test
ownerIDValid=true;
Integer ownrId = null;
try {
ownrId = Integer.parseInt(ownerId);
Owner sel_owner = this.clinicService.findOwnerById(ownrId);//jim added this
if (sel_owner == null) ownerIDValid = false;
model.put("sel_owner",sel_owner);
catch( Exception ex) {
ownerIDValid = false;
}
}
model.put("ownerIDValid", ownerIDValid);
return "owners/ownersList";
}
公共字符串processFindForm(@RequestParam(value=“ownerID”,required=false)字符串ownerID、所有者所有者、BindingResult结果、映射模型){
收集结果=this.clinicService.findOwnerByLastName(“”);
模型。放置(“选择”,结果);
布尔ownerIDValid;
如果(ownerId==null){ownerIDValid=false;}
else{//我添加了这个if测试
ownerIDValid=true;
整数ownrId=null;
试一试{
ownrId=Integer.parseInt(ownerId);
Owner sel_Owner=this.clinicService.findOwnerById(ownrId);//jim添加了这个
如果(sel_owner==null)ownerIDValid=false;
模型出售(“卖方所有人”,卖方所有人);
捕获(例外情况除外){
ownerIDValid=false;
}
}
模型.put(“ownerIDValid”,ownerIDValid);
返回“所有者/所有者列表”;
}
以下是ownersList.jsp:
<html><head>tags and stuff</head>
<body>
<div class="container">
<table>
<tr>
<td></td>
<td>
<spring:url value="/owners/new" var="owner_newUrl"></spring:url>
<a href="${fn:escapeXml(owner_newUrl)}" class="btn btn-info" >Add New Owner</a>
</td>
</tr>
<tr>
<td width=160 valign="top">
<datatables:table id="owners" data="${selections}" cdn="true" row="owner" theme="bootstrap2"
cssClass="table table-striped" paginate="true" info="false"
cssStyle="width: 150px;" align="left" >
<datatables:column title="Name" cssStyle="width: 150px;" display="html">
<spring:url value="/owners?ownerID={ownerId}" var="ownerUrl">
<spring:param name="ownerId" value="${owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(ownerUrl)}"><c:out value="${owner.firstName} ${owner.lastName}"/></a>
</datatables:column>
</datatables:table>
</td>
<c:if test="${ownerIDValid}">
<td valign="top">
<table class="table table-striped" style="width:600px;">
<tr>
<td><b><c:out value="${sel_owner.firstName} ${sel_owner.lastName}"/></b></td>
<td></td>
</tr>
<tr>
<td><c:out value="${sel_owner.address}"/></td>
<td><c:out value="${sel_owner.city}"/></td>
</tr>
<tr>
<td><c:out value="${sel_owner.telephone}"/></td>
<td></td>
</tr>
<tr>
<td colspan=2>
<spring:url value="/owners/{ownerId}/edit" var="owner_editUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_editUrl)}" class="btn btn-info" >Edit This Owner</a>
<spring:url value="/owners/{ownerId}/pets/new" var="owner_newpetUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_newpetUrl)}" class="btn btn-info" >Add New Pet</a>
<spring:url value="/owners?ownerID={ownerId}&type=cats" var="owner_catsUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_catsUrl)}" class="btn btn-info" >Show Cats</a>
<spring:url value="/owners?ownerID={ownerId}&type=dogs" var="owner_dogsUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_dogsUrl)}" class="btn btn-info" >Show Dogs</a>
<spring:url value="/owners?ownerID={ownerId}&type=all" var="owner_allUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_allUrl)}" class="btn btn-info" >Show All Pets</a>
</td>
</tr>
<c:if test="${fn:endsWith(requestScope['javax.servlet.forward.query_string'], 'all')}">
<tr>
<td colspan=2>
<p>Pets</p>
<datatables:table id="pets" data="${sel_owner.pets}" cdn="true" row="pet" theme="bootstrap2"
cssClass="table table-striped" paginate="false" info="false" filter="false"
cssStyle="width: 350px;" align="left" >
<datatables:column title="Name" cssStyle="width: 200px;" display="html">
<c:out value="${pet.name}"/>
</datatables:column>
<datatables:column title="BirthDate" cssStyle="width: 300px;" display="html">
<joda:format value="${pet.birthDate}" pattern="yyyy-MM-dd"/>
</datatables:column>
<datatables:column title="Type" cssStyle="width: 200px;" display="html">
<c:out value="${pet.type.name}"/>
</datatables:column>
</datatables:table>
</td>
</tr>
</c:if>
<c:if test="${fn:endsWith(requestScope['javax.servlet.forward.query_string'], 'cats')}">
<tr>
<td colspan=2>
<p>Cats</p>
<datatables:table id="cats" data="${sel_owner.cats}" cdn="true" row="cat" theme="bootstrap2"
cssClass="table table-striped" paginate="false" info="false" filter="false"
cssStyle="width: 350px;" align="left" >
<datatables:column title="Name" cssStyle="width: 200px;" display="html">
<c:out value="${cat.name}"/>
</datatables:column>
<datatables:column title="BirthDate" cssStyle="width: 300px;" display="html">
<joda:format value="${cat.birthDate}" pattern="yyyy-MM-dd"/>
</datatables:column>
<datatables:column title="Type" cssStyle="width: 200px;" display="html">
<c:out value="${cat.type.name}"/>
</datatables:column>
</datatables:table>
</td>
</tr>
</c:if>
<c:if test="${fn:endsWith(requestScope['javax.servlet.forward.query_string'], 'dogs')}">
<tr>
<td colspan=2>
<p>Dogs</p>
<datatables:table id="dogs" data="${sel_owner.dogs}" cdn="true" row="dog" theme="bootstrap2"
cssClass="table table-striped" paginate="false" info="false" filter="false"
cssStyle="width: 350px;" align="left" >
<datatables:column title="Name" cssStyle="width: 200px;" display="html">
<c:out value="${dog.name}"/>
</datatables:column>
<datatables:column title="BirthDate" cssStyle="width: 300px;" display="html">
<joda:format value="${dog.birthDate}" pattern="yyyy-MM-dd"/>
</datatables:column>
<datatables:column title="Type" cssStyle="width: 200px;" display="html">
<c:out value="${dog.type.name}"/>
</datatables:column>
</datatables:table>
</td>
</tr>
</c:if>
</table>
</td>
</tr>
</c:if>
<c:if test="${!ownerIDValid}">
Print this content if there is no ownerID.
</c:if>
</table>
</div>
</body>
</html>
标签和东西
宠物
@RequestMapping(value = "/owners", method = RequestMethod.GET)
public String processFindForm(Owner owner, BindingResult result, Map<String, Object> model)
{
String ownerId = DEFAULT value;
}
@RequestMapping(value = "/owners/{ownerID}", method = RequestMethod.GET)
public String processFindForm(@RequestParam("ownerID") String ownerId, Owner owner, BindingResult result, Map<String, Object> model)
{
}
@RequestMapping(value = { "/owners", "/owners/{ownerID}" }, method = RequestMethod.GET)
public String processFindForm(@RequestParam(value="ownerID", required=false) String ownerId, Owner owner, BindingResult result, Map<String, Object> model)
{
}