简体   繁体   English

如何在Spring 2.x中将结果发送回

[英]how send result back in Spring 2.x

I have a method which returns true or false based on some parameters. 我有一个根据某些参数返回true或false的方法。 So I make an ajax call (using Ext.ajax.request). 因此,我进行了ajax调用(使用Ext.ajax.request)。 In spring 2.x version how do I send back the result? 在Spring 2.x版本中,如何发回结果? So for my controller I extend BaseSimpleCommandController and override the method 因此,对于我的控制器,我扩展了BaseSimpleCommandController并覆盖了该方法

ModelAndView doExecute(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)

I want to know what would i need to do to send back just a boolean value. 我想知道我该怎么做才能发回布尔值。 I am little confused as what needs to be done. 我对需要做什么感到困惑。 I now i have to send back a ModelAndView type object but nor sure i should i embed a single boolean value in to this object. 现在,我必须发送回ModelAndView类型的对象,但也不确定我是否应该将单个布尔值嵌入此对象。

EDIT: the BaseSimpleCommandController is specific to my project which in turn extends AbstractCommandController from spring. 编辑: the BaseSimpleCommandController特定于我的项目,而该项目又从Spring扩展了AbstractCommandController sorry for the confusion. 对困惑感到抱歉。

If you just want to return "true" or "false" there is no need to use models, command controllers, etc. Simply inject HttpServletResponse and send the data directly: 如果只想返回"true""false" ,则无需使用模型,命令控制器等。只需注入HttpServletResponse并直接发送数据即可:

public void handle(HttpServletResponse response) {
  boolean flag = //...
  response.getWriter().print(flag);
}

Request params can be mapped via annotation in the method parameter, or through the request's parameter map 可以通过方法参数中的注释或请求的参数映射来映射请求参数

Method Parameter: 方法参数:

@RequestMapping(method=RequestMethod.GET)
public void someCall(@RequestParam(value="param1") String paramName)
...

Where param1 would be the get parameter param1. 其中param1将是get参数param1。 If you don't provide a value for the annotation, it tries to bind to the name of the parameter in the method name (paramName in this case). 如果您没有为注释提供值,它将尝试绑定到方法名称中的参数名称(在这种情况下为paramName)。

Parameter Map 参数图

@RequestMapping(method=RequestMethod.GET)
public void someCall(HttpServletRequest request)
{
    Map<String, String[]> paramMap = request.getParameterMap();
}

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM