Spring-MVC单例中的所有控制器是否都在不同的会话和请求之间共享?
如果是这样,我假设一个类变量就好
public String name;
Run Code Online (Sandbox Code Playgroud)
对于所有请求和会话都是一样的吗?因此,如果用户X发出请求并且name正在设置为Paul,则用户Z还将Paul作为属性?
在我的情况下,我不希望这种行为,但想知道是否有一个更简单,或更清洁的OOP方式,以获得会话/请求变量然后session.getAttribute()/request.getAttribute()
我使用超过6个月的春天.我无法理解与下面场景相关的这种潜在机制.
我有一个春季网络应用程序.现在我在控制器中自动启动了模型.基于url匹配,它调用各自的方法.我所有的方法都是单身.
现在当两个用户同时打开app时,spring可以并行运行它们并为它们提供结果.我不明白它怎么能这样做.我的意思是,因为bean是单例,它必须等待直到不使用bean或覆盖bean中的数据.但春天工作正常.有人可以用一些类比来解释这种行为.
为了解释我的问题,下面是一段代码:
我的默认控制器很简单:
@Autowired
private AppModel aModel;
public AppModel getModel(){
return aModel;
}
public void setModel(AppModel aModel){
this.aModel = aModel;
}
@RequestMapping(method = RequestMethod.GET)
public ModelAndView defaultGetter(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView(getViewName());
mav.addObject("model", aModel);
Runtime.getRuntime().gc();
return mav;
}
Run Code Online (Sandbox Code Playgroud)
也可以有人告诉我,当两个客户打开应用程序时,我会使用@autowired生成两个单独的模型.如果所有客户端只存在一个模型bean,那么请说客户端1的请求进来,我需要30秒才能获得结果.现在,如果第二个客户端在第3秒发送请求,那么第一个客户端请求是否会被覆盖?
我想我很困惑.有人可以澄清这种魔法是如何发生的吗?
谢谢
如果我有这样的HTML <form>:
<input type="text" value="someValue1" name="myValues"/>
<input type="text" value="someValue2" name="myValues"/>
<input type="text" value="someValue3" name="myValues"/>
<input type="text" value="someValue4" name="myValues"/>
Run Code Online (Sandbox Code Playgroud)
我知道在servlet中我可以使用以下方法获取值:
String[] values = request.getParameterValues("myValues");
Run Code Online (Sandbox Code Playgroud)
如何使用Spring MVC做类似的事情?
有没有一种方法可以获取整个查询字符串而不进行解析?如:
http://localhost:8080/spring-rest/ex/bars?id=100,150&name=abc,efg
Run Code Online (Sandbox Code Playgroud)
我想得到一切吗?作为一个字符串。是的,我稍后会对其进行解析,但这使我的控制器和所有后续代码更加通用。
到目前为止,我已经尝试使用@ PathParam,@ RequestParam以及@Context UriInfo,结果如下。但是我似乎无法理解整个字符串。这就是我要的:
id=100,150&name=abc,efg
Run Code Online (Sandbox Code Playgroud)
使用curl @PathParam使用
http:// localhost:8080 / spring-rest / ex / bars / id = 100,150&name = abc,efg
产生id = 100,150
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/spring-rest/ex/qstring/{qString}")
public String getStuffAsParam ( @PathParam("qstring") String qString) {
...
}
Run Code Online (Sandbox Code Playgroud)
@RequestParam使用
http:// localhost:8080 / spring-rest / ex / bars?id = 100,150&name = abc,efg
给出无法识别的名称。
http:// localhost:8080 / spring-rest / ex / bars?id = 100,150; name = abc,efg
产生异常。
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/spring-rest/ex/qstring")
public String getStuffAsMapping (@RequestParam (value ="qstring", required = false) …Run Code Online (Sandbox Code Playgroud) 我是Spring MVC Framework的初学者.两天前我开始学习Spring.出于学习目的,我正在开发一个简单的应用程 即,从表单获取用户输入并在另一页面中显示值.我得到了一个异常"java.lang.IllegalStateException:BindingResult和bean name'命令的普通目标对象'都不可用作请求属性".我无法弄清楚我的代码中有什么问题.我搜索谷歌并尝试了很多解决方案,但问题仍然存在.
这是我的视图index.jsp
<form:form action="/addDisplay" method="POST">
<form:label path="name"><h3>Name</h3></form:label>
<form:input type="text" path="name" cssClass="form-control text-center" required="required"/>
<form:label path="age"><h3>Age</h3></form:label>
<form:input type="number" path="age" cssClass="form-control text-center" required="required"/>
<form:label path="work"><h3>Work Place</h3></form:label>
<form:input type="text" path="work" cssClass="form-control text-center" required="required"/>
<form:label path="designation"><h3>Designation</h3></form:label>
<form:input type="text" path="designation" cssClass="form-control text-center" required="required"/>
<form:label path="area"><h3>Area</h3></form:label>
<form:input type="text" path="area" placeholder="Where Are You From?" cssClass="form-control text-center" required="required"/>
<form:label path="mobile"><h3>Mobile Number</h3></form:label>
<form:input type="number" path="mobile" placeholder="Your Mobile Number.!" cssClass="form-control text-center" required="required"/>
<form:label path="email"><h3>Email</h3></form:label>
<form:input type="email" path="email" placeholder="Your Email Id..!" cssClass="form-control text-center" required="required"/>
<br/>
<input type="submit" …Run Code Online (Sandbox Code Playgroud) 我可以在Spring MVC的Controller类中获取HttpRequest对象吗?
@Controller
public class ContactController {
@Autowired
private ContactService contactService;
@RequestMapping("/login")
public String displayLoginPage(@ModelAttribute("login") Login login, BindingResult result) {
return "login";
}
}
Run Code Online (Sandbox Code Playgroud) 我可以HttpServletRequest在我的RestController类似下面自动装配吗,servletRequest即使它在高度并发的环境中执行,它也会返回不同的结果。我有一个限制,我不能作为方法参数,因为我正在实现一个自动生成的接口,不会HttpServletRequest作为方法参数。
@RestController
public class MyController implements MyInterface {
@Autowired
private HttpServletRequest servletRequest;
@Override
@RequestMapping(value = "/test", produces = {"application/json"}, consumes = {"application/json"}, method = RequestMethod.POST)
public ResponseEntity<MyResponse> test(@RequestBody final MyRequest payload){
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
我已经浏览了这些 SO 问题和其他一些关于此的文章。但只是想确保当我们HttpServletRequest在控制器中自动装配时,它的Scope是Request?
Spring 3 MVC 从控制器访问 HttpRequest
如何在 Spring bean 中获取 HttpServletRequest?
注意:我确实尝试过这个,它似乎工作正常。但只是想确认即使在高度并发的环境中它也是一个万无一失的解决方案。此外,如果这是正确的方法,如果有人能解释它的工作原理,我将不胜感激。