weixin_42236468 2008-08-28 15:45
浏览 156
已采纳

spring mvc的问题

spring2.5.5,我怎样才能不返回一个页面,返回一个null?

return "common/county";
我试了void,返回一个默认的county.jsp;return null也不行。

[code="java"]
@Controller
public class CountyController {
@Autowired
private CommonService commonService;
@RequestMapping("/county.html")
public String listItems(Model model, County county, HttpServletResponse response){
if(county.getID() == 0){
county.setID(1);
}
county = commonService.getCounty(county.getID());
StringBuffer sb = CountyUtil.spliceCounties(new ArrayList(county.getChildren()), county);
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("text/html; charset=GBK");
out.println(sb.toString());
return "common/county";
}
}
[/code]

mvc配置文件
[code="java"]



[/code]

  • 写回答

2条回答 默认 最新

  • iteye_14762 2008-08-28 20:40
    关注

    使用annotation不返回一个页面, 要在你的方法中传入一个ServletResponse, OutputStream, 或者Writer参数:
    [code="java"]
    public void otherView(HttpServletResponse response) {...}

    public void otherView(OutputStream os) {...}

    public void otherView(Writer writer) {...}

    [/code]
    上面三个都行, 注意返回值为void

    如果返回值不为void, 你则需要将response等在你的方法中close掉:
    [code="java"]
    public void otherView(HttpServletResponse response) {
    response.getWriter().close()
    }
    ....
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波