我不确定我是否完全理解该问题,但是两种模式是***实践:
I'm not sure I fully understand the problem, but two patterns are best practices:
- 总是经过一个控制器,该控制器填充模型,将其存储在请求中,然后分派到一个视图,该视图在模型中显示数据.这就是MVC模式
- 总是在成功的非幂等请求(即POST,如果遵守HTTP协议)之后重定向.那就是后重定向获取模式.
所以这意味着您应该拥有:
So what it means is that you should have:
- 请求1进入servlet.
- servlet获取要在表单中显示的数据并将其存储在请求中,然后转发到JSP
- JSP显示表单
- 将表单提交到servlet(请求2)
- servlet将数据存储在数据库中,该数据库将为创建的数据生成一个ID
- servlet重定向到
/product?id=<generatedId>
或/product/<generatedId>
之类的URL
- 浏览器向该URL发送一个请求(请求3).该请求发送到servlet
- servlet从数据库获取ID标识的数据.它将数据存储到请求中,然后转发到JSP
- JSP显示数据.
- request 1 goes to a servlet.
- servlet gets data to be displayed in the form and stores it in the request, then forwards to the JSP
- the JSP displays the form
- the form is submitted to a servlet (request 2)
- servlet stores the data in the database, which generates an ID for the created data
- servlet redirects to a URL like
/product?id=<generatedId>
or /product/<generatedId>
- browser sends a request to this URL (request 3). This request goes to a servlet
- servlet gets the data identified by the ID, from the database. It stores the data into the request, and forwards to a JSP
- the JSP displays the data.
当然,您可以选择重定向到其他页面,例如产品列表.
Of course, you could choose to redirect to some other page, like the list of products for example.
如果麻烦的是在从servlet传输到JSP时使用请求存储数据,那么这应该不会打扰您:这是唯一的干净方法.数据将仅在请求范围内,并在处理请求后被垃圾回收.
If what bothers you is to use the request to store data when forwarding from the servlet to the JSP, then that shouldn't bother you: it's the only clean way to do it. The data will be scoped to the request only, and be garbage-collected when the request has been processed.