MVC中控制层调用服务层的方法,方法有返回值,但是页面显示为空

orange_jli 2017-12-16 02:38:33

主要构想是从页面中用http session传输name这个值到控制层进行查询数据库操作,然后返回查询到的数据到B页面,B页面用session展示出来,现在AB页面都做好了,就是调用GETBYPORP方法不行,说我的值没有装载进对象。怎么办。



A页面
<#assign name='小米手机6' />
<input type="text" id="name" name="name" value=“${name}" hidden />
<input type="text" id="releaseTime" name="releaseTime" hidden />
<input type="text" id="processor" name="processor" hidden />
<input type="text" id="screenMaterial" name="screenMaterial" hidden />
<input type="text" id="batteryCapacity" name="batteryCapacity" hidden />
<input type="text" id="RAM" name="RAM" hidden />
<input type="text" id="ROM" name="ROM" hidden />
<input type="text" id="price" name="price" hidden />
<input type="text" id="information" name="information" hidden />
<script>
// var i = 0;
var btnSend =document.getElementById("send");
btnSend.onclick = function () {
var valname=document.getElementById("name").value;
var valreleaseTime=document.getElementById("releaseTime").value;
var valprocessor=document.getElementById("processor").value;
var valscreenMaterial=document.getElementById("screenMaterial").value;
var valbatteryCapacity=document.getElementById("batteryCapacity").value;
var valRAM=document.getElementById("RAM").value;
var valROM=document.getElementById("ROM").value;
var valprice=document.getElementById("price").value;
var valinformation=document.getElementById("information").value;
var xmlhttp = new XMLHttpRequest();
// 2 监控请求状态变化
xmlhttp.onreadystatechange = function () {
// 请求成功返回
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
// 验证成功
if (xmlhttp.responseText == "yes") {
alert("OK");
// 跳转到详情页面
window.location.href = "/cs";
} else {
alert("失败");
// info.innerHTML = "错误的账号或密码";
// info.style.color = "red";
}
}
}
// 3 设置请求参数
xmlhttp.open("GET", "/goods/send?name="+ valname+ "&&releaseTime=" + valreleaseTime
+ "&&processor=" + valprocessor+ "&&screenMaterial=" + valscreenMaterial
+ "&&batteryCapacity=" + valbatteryCapacity+ "&&RAM=" + valRAM+ "&&ROM=" + valROM
+ "&&price=" + valprice+ "&&information=" + valinformation, true);
// 4 异步请求
xmlhttp.send(null);

}


B页面



<#if Session["goods"]?exists>
${Session.goods.name!""}
${Session.goods.releaseTime!""}
${Session.goods.processor!""}
${Session.goods.screenMaterial!""}
${Session.goods.batteryCapacity!""}
${Session.goods.RAM!""}
${Session.goods.ROM!""}
${Session.goods.price!""}
</#if>



控制层

@RequestMapping(value = "/send")
@ResponseBody
public String send(
@RequestParam String name,
@RequestParam String releaseTime,
@RequestParam String processor,
@RequestParam String screenMaterial,
@RequestParam String batteryCapacity,
@RequestParam String RAM,
@RequestParam String ROM,
@RequestParam String price,
@RequestParam String information,
HttpServletRequest request) {
String flag = "no";
Map map = new HashMap(); //new一个新集合来装载信息
map.put("name", name);
map.put("releaseTime", releaseTime);
map.put("processor", processor);
map.put("screenMaterial", screenMaterial);
map.put("batteryCapacity", batteryCapacity);
map.put("RAM", RAM);
map.put("ROM", ROM);
map.put("price", price);
map.put("information", information);
Goods goods = gs.getByProp(map); //服务层的方法要返回值,否则goods值就为空
if (goods != null) {
// 将对象写进session
HttpSession session = request.getSession();
session.setAttribute("goods", goods);
flag = "yes";
}
return flag;
}


方法

public Goods getByProp(Map prop) {
return igd.getByProp(prop);
}


XML

<select id="getByProp" resultType="com.example.demo.dto.Goods" parameterType="java.util.Map">
SELECT
<include refid="all"/>
FROM
<include refid="table"/>
<where>
<if test="name!=null">
name=#{name}
</if>
</where>
</select>

...全文
384 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
orange_jli 2018-04-08
  • 打赏
  • 举报
回复
我自己来解决吧,是因为我调用的关键字不是唯一参数,数据库有相同参数输出导致错误。所以只需要把嵌入的关键字改为唯一参数就行。
orange_jli 2017-12-17
  • 打赏
  • 举报
回复
orange_jli 2017-12-16
  • 打赏
  • 举报
回复
没有人理我一下吗。

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧