Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to deleting object after each response no need to session,</p> <p>If you want keep object during user session , It has some ways:</p> <ol> <li><p>directly add one attribute to session:</p> <pre><code>@RequestMapping(method = RequestMethod.GET) public String testMestod(HttpServletRequest request){ ShoppingCart cart = (ShoppingCart)request.getSession().setAttribute("cart",value); return "testJsp"; } </code></pre> <p>and you can get it fro controller like this :</p> <pre><code>ShoppingCart cart = (ShoppingCart)session.getAttribute("cart"); </code></pre></li> <li><p>Make your controller session scoped</p> <pre><code>@Controller @Scope("session") </code></pre></li> <li><p>Scope the Objects ,for example you have user object that want be in session every time:</p> <pre><code>@Component @Scope("session") public class User { String user; /* setter getter*/ } </code></pre> <p>then inject class in each controller that you want</p> <pre><code> @Autowired private User user </code></pre> <p>that keep class on session.</p></li> <li><p>The AOP proxy injection : in spring -xml:</p> <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"&gt; &lt;bean id="user" class="com.User" scope="session"&gt; &lt;aop:scoped-proxy/&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>then inject class in each controller that you want</p> <pre><code>@Autowired private User user </code></pre></li> </ol> <p>5.Pass HttpSession to method: </p> <pre><code> String index(HttpSession session) { session.setAttribute("mySessionAttribute", "someValue"); return "index"; } </code></pre> <p>6.Make ModelAttribute in session By @SessionAttributes("ShoppingCart"):</p> <pre><code> public String index (@ModelAttribute("ShoppingCart") ShoppingCart shoppingCart, SessionStatus sessionStatus) { //Spring V4 //you can modify session status by sessionStatus.setComplete(); } </code></pre> <p>or you can add Model To entire Controller Class Such </p> <pre><code>@Controller @SessionAttributes("ShoppingCart") @RequestMapping("/req") public class MYController { @ModelAttribute("ShoppingCart") public Visitor getShopCart (....) { return new ShoppingCart(....); //get From DB Or Session } } </code></pre> <p>each one has advantage and disadvantage:</p> <p>@session may use more memory in cloud systems it copies session to all nodes, and direct method (1 and 5) has messy approach, it is not good to unit test.</p> <p>To access session jsp </p> <pre><code>&lt;%=session.getAttribute("ShoppingCart.prop")%&gt; </code></pre> <p>in Jstl :</p> <pre><code>&lt;c:out value="${sessionScope.ShoppingCart.prop}"/&gt; </code></pre> <p>in Thymeleaf:</p> <pre><code>&lt;p th:text="${session.ShoppingCart.prop}" th:unless="${session == null}"&gt; . &lt;/p&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload