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]
使用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]
第二个返回不为void:
[code="java"]
public String otherView(HttpServletResponse response) {
response.getWriter().close()
}
....
[/code]