jeesite icon indicating copy to clipboard operation
jeesite copied to clipboard

either BindingResult nor plain target object for bean name 'tRestaurant' available as request attribute

Open sheldonshen opened this issue 9 years ago • 10 comments

自动生成代码如下:
@RequiresPermissions("spzx:tRestaurant:view")
	@RequestMapping(value = {"list", ""})
	public String list(TRestaurant tRestaurant, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TRestaurant> page = tRestaurantService.findPage(new Page<TRestaurant>(request, response), tRestaurant); 
		model.addAttribute("page", page); 	
		return "modules/spzx/tRestaurantList";
	}

可是到达tRestaurantList.jsp页面时,就抛出这个错误,添加一行代码,如下:

@RequiresPermissions("spzx:tRestaurant:view")
	@RequestMapping(value = {"list", ""})
	public String list(TRestaurant tRestaurant, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TRestaurant> page = tRestaurantService.findPage(new Page<TRestaurant>(request, response), tRestaurant); 
		model.addAttribute("page", page); 	
		model.addAttribute("tRestaurant",tRestaurant);//手动添加的一行代码
		return "modules/spzx/tRestaurantList";
	}

就好了,我的疑问是,系统中的最开始就有的代码,如UserController.java:

@RequiresPermissions("sys:user:view")
	@RequestMapping(value = {"list", ""})
	public String list(User user, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<User> page = systemService.findUser(new Page<User>(request, response), user);
        model.addAttribute("page", page);
		return "modules/sys/userList";
	}

也没有添加这行代码,却没有问题,请问作者这是什么原因?

sheldonshen avatar Apr 21 '17 09:04 sheldonshen

因为userList中的user对象是从page中取出来的<c:forEach items="${page.list}" var="user">

javyuan avatar Apr 21 '17 09:04 javyuan

你好,首先谢谢你的热心回答,我先贴代码.

<form:form id="searchForm" modelAttribute="tRestaurant" action="${ctx}/spzx/tRestaurant/" method="post" class="breadcrumb form-search">
		<input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
		<input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
		<ul class="ul-form">
			<li><label>品牌id:</label>
				<form:input path="brandId" htmlEscape="false" maxlength="11" class="input-medium"/>
			</li>
			<li><label>餐厅名字:</label>
				<form:input path="name" htmlEscape="false" maxlength="100" class="input-medium"/>
			</li>
			<li><label>电话:</label>
				<form:input path="tel" htmlEscape="false" maxlength="20" class="input-medium"/>
			</li>
			<li><label>是否开启餐位费 0开启 1关闭:</label>
				<form:radiobuttons path="operSeatCast" items="${fns:getDictList('yes_no')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
			</li>
			<li><label>餐位费:</label>
				<form:input path="seatCast" htmlEscape="false" class="input-medium"/>
			</li>
			<li><label>人均消费:</label>
				<form:input path="avgConsume" htmlEscape="false" class="input-medium"/>
			</li>
			<li><label>合作开始时间:</label>
				<input name="cooperateStartTime" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate"
					value="<fmt:formatDate value="${tRestaurant.cooperateStartTime}" pattern="yyyy-MM-dd HH:mm:ss"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false});"/>
			</li>
			<li><label>合作结束时间:</label>
				<input name="cooperateEndTime" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate"
					value="<fmt:formatDate value="${tRestaurant.cooperateEndTime}" pattern="yyyy-MM-dd HH:mm:ss"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false});"/>
			</li>
			<li><label>综合评分:</label>
				<form:input path="allScore" htmlEscape="false" class="input-medium"/>
			</li>
			<li><label>环境评分:</label>
				<form:input path="enviroScore" htmlEscape="false" class="input-medium"/>
			</li>
			<li><label>服务评分:</label>
				<form:input path="serviceScore" htmlEscape="false" class="input-medium"/>
			</li>
			<li><label>是否启用 0启用 1禁用:</label>
				<form:radiobuttons path="isdisable" items="${fns:getDictList('yes_no')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
			</li>
			<li class="btns"><input id="btnSubmit" class="btn btn-primary" type="submit" value="查询"/></li>
			<li class="clearfix"></li>
		</ul>
	</form:form>

之所以抛这个异常,是由于以上代码中modelAttribute="tRestaurant"属性取不到值,对应到UserController中的代码如下:

<form:form id="searchForm" modelAttribute="user" action="${ctx}/sys/user/list" method="post" class="breadcrumb form-search ">
		<input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
		<input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
		<sys:tableSort id="orderBy" name="orderBy" value="${page.orderBy}" callback="page();"/>
		<ul class="ul-form">
			<li><label>归属公司:</label><sys:treeselect id="company" name="company.id" value="${user.company.id}" labelName="company.name" labelValue="${user.company.name}" 
				title="公司" url="/sys/office/treeData?type=1" cssClass="input-small" allowClear="true"/></li>
			<li><label>登录名:</label><form:input path="loginName" htmlEscape="false" maxlength="50" class="input-medium"/></li>
			<li class="clearfix"></li>
			<li><label>归属部门:</label><sys:treeselect id="office" name="office.id" value="${user.office.id}" labelName="office.name" labelValue="${user.office.name}" 
				title="部门" url="/sys/office/treeData?type=2" cssClass="input-small" allowClear="true" notAllowSelectParent="true"/></li>
			<li><label>姓&nbsp;&nbsp;&nbsp;名:</label><form:input path="name" htmlEscape="false" maxlength="50" class="input-medium"/></li>
			<li class="btns"><input id="btnSubmit" class="btn btn-primary" type="submit" value="查询" onclick="return page();"/>
				<input id="btnExport" class="btn btn-primary" type="button" value="导出"/>
				<input id="btnImport" class="btn btn-primary" type="button" value="导入"/></li>
			<li class="clearfix"></li>
		</ul>
	</form:form>

我手动添加一行代码,

model.addAttribute("tRestaurant",tRestaurant);//手动添加的一行代码

modelAttribute="tRestaurant"属性就取到值了,可是UserController中却没有类似的代码,modelAttribute="user"却可以取到值,这就是我的疑问.

sheldonshen avatar Apr 24 '17 00:04 sheldonshen

把userConsoller里model中org.springframework.validation.BindingResult.user删掉后应该会报和你同样的错误,不过还没找到这个值是在什么放到model里的。

public String list(HttpServletRequest request, HttpServletResponse response, Model model) { BindingAwareModelMap model2 = (BindingAwareModelMap)model; model2.remove("org.springframework.validation.BindingResult.user"); return "modules/sys/userList"; }

javyuan avatar Apr 24 '17 04:04 javyuan

UserController中有@ModelAttribute,你看下你的controller里有没有

@ModelAttribute
public User get(@RequestParam(required=false) String id) {
	if (StringUtils.isNotBlank(id)){
		return systemService.getUser(id);
	}else{
		return new User();
	}
}

javyuan avatar Apr 24 '17 06:04 javyuan

你好,经过你的指点,弄懂了UserController对应的jsp页面不抛错的原因了,

@ModelAttribute
	public User get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return systemService.getUser(id);
		}else{
			return new User();
		}
	}

先执行这段代码,将bean放到了model中,之后再执行

@RequiresPermissions("sys:user:view")
	@RequestMapping(value = {"list", ""})
	public String list(User user, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<User> page = systemService.findUser(new Page<User>(request, response), user);
        model.addAttribute("page", page);
		return "modules/sys/userList";
	}

跳转到userList页面时,model中已有对应的bean了,

sheldonshen avatar Apr 24 '17 10:04 sheldonshen

我的controller里是有这个方法的,代码如下:

@ModelAttribute
	public TRestaurant get(@RequestParam(required=false) String id) {
		TRestaurant entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tRestaurantService.get(id);
		}
		if (entity == null){
			entity = new TRestaurant();
		}
		return entity;
	}

所以应该是执行list前,没有执行这个方法.暂时原因不明.

sheldonshen avatar Apr 24 '17 10:04 sheldonshen

你好,我刚调试了下我的controller,在执行list方法前,是执行这个get方法的,觉得现在唯一的解决方法就是要调试spring的源码,来找到原因为什么没有放到model中.

sheldonshen avatar Apr 24 '17 11:04 sheldonshen

嗯,调试一下,spring源码也不难只是很乱,不要怕调试他的源码。

javyuan avatar Apr 25 '17 02:04 javyuan

原因找到了,在我的controller中的这段代码

@ModelAttribute
	public TRestaurant get(@RequestParam(required=false) String id) {
		TRestaurant entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tRestaurantService.get(id);
		}
		if (entity == null){
			entity = new TRestaurant();
		}
		return entity;
	}

等价于

model.addAttribute("TRestaurant",tRestaurant);//手动添加的一行代码

注意,不等价于

model.addAttribute("tRestaurant",tRestaurant);//手动添加的一行代码

所以,在对应的jsp页面只需要把

	<form:form id="searchForm" modelAttribute="tRestaurant" action="${ctx}/spzx/tRestaurant/" method="post" class="breadcrumb form-search">

修改为

	<form:form id="searchForm" modelAttribute="TRestaurant" action="${ctx}/spzx/tRestaurant/" method="post" class="breadcrumb form-search">

就可顺利的取到值了.和 #406 的原因是一样的.

sheldonshen avatar Apr 26 '17 06:04 sheldonshen

哈哈,牛逼

javyuan avatar Apr 26 '17 09:04 javyuan