How can I save a string in a session using Spring MVC?

I am new to Spring MVC and I am trying to find a suitable way to do something that I think is very simple.

I have a simple jQuery AJAX call:

    var dataString = 'existingProject='+ $("#existingProject").val() + '&newProjName=' +      $("#newProjName").val();

    $.ajax({  
        type: "POST",  
        url: "manageProjects.html",  
        data: dataString            
    });

      

I want to set this custom "project" parameter for the session for the user. This AJAX call exits the javascript in the JS file and connects to my Spring MVC controller.

The controller receives these variables, but I'm not sure what to do with this so that this data is passed in the session.

I searched for a heck from this thread and came across 6 different options (introduced session space beans using autowiring, beans with xml config and cgl-nodep libraries, HttpServlet attributes, @ModelAttribute, @SessionAttribute, etc.). I tried to connect to the beans session and defined the following:

@Component
@Scope("session")
public class UserSettings

      

... But the bean was not blocked prior to the session. I used @Autowire in my controller and found it was the same instance between sessions, so I obviously messed it up.

I just want to keep one exciting line! There should be level 101 an easy way to do it ...

+3


source to share


1 answer


@RequestMapping(value = "/request/mapping")
public ModelAndView methodName(HttpSession session,...){

session.setAttribute("testVariable", "Test Values!!");
}

      

or



@RequestMapping("/test")
@Controller
public class TestController {
    @RequestMapping(method = RequestMethod.GET)
    public String testMestod(HttpServletRequest request)
    {
        request.getSession().setAttribute("testVariable", "Test Values!!");
        return "testJsp";
    }
}

      

+10


source







All Articles