简体   繁体   English

spring-mvc:如何在控制器中不使用方法参数的情况下在mvc中传递参数

[英]spring-mvc : how to pass parameters in mvc without method arguments at the controller

I was being tasked to create my controllers without passing any parameters at the method signature. 我的任务是创建我的控制器, 而无需在方法签名处传递任何参数 I am quite baffled as this is quite challenge and I haven't really seen any examples at the net. 我很困惑,因为这是一个很大的挑战,而且我在网络上还没有真正看到任何例子。 For example: from my jsp page. 例如:从我的jsp页面。

    <form:form method="POST" action="/project/searchResults" id="productSearchResultsForm">
        <input name="productRecord" id="productRecord" />
        <input name="resultEntity" id="resultEntity" type="hidden" />
        <input name="resultCode" id="resultCode" type="hidden" />
        <button type="submit" class="btn btn-primary btn-lg">Submit</button>
    </form:form>

I wanted to pass these three inputs to my controller without using @RequestParameter and @ModelAttribute at the method signature. 我想将这三个输入传递给控制器​​,而不在方法签名处使用@RequestParameter和@ModelAttribute。 As of now this is the only way I know how to do this. 到目前为止,这是我知道如何执行此操作的唯一方法。

@RequestMapping(value = "/productSearch", method = RequestMethod.GET)
public ModelAndView init(@ModelAttribute ("showroomCode") String showroomCode, HttpServletRequest request) {


    logger.info("<<<< initial page <<<<<<");
    logger.info("Showroom Code : " + showroomCode);

    HttpSession session = request.getSession();

    ModelAndView model = new ModelAndView("productSearch", "command",
            String.class);


     ShowroomUser user = new ShowroomUser();
     user.setUserId(1);
     session.setAttribute("usersession", user);
     session.setAttribute("showroomCode", showroomCode);

     logger.info(">>>>> initial page >>>>> ");
     return model;
}

I know that in Struts 2, you can just declare global variables in the Action class so you can automatically use them in the method without literally putting them in the method signature. 我知道在Struts 2中,您可以在Action类中声明全局变量,这样您就可以在方法中自动使用它们,而不必将它们放在方法签名中。 Is there are way to remove this? 有办法删除吗?

Thanks. 谢谢。

Update 更新

I am asked not to use HttpServlet Request and Model Attributes at all anywhere in the code. 我被要求不要在代码中的任何地方都使用HttpServlet请求和模型属性。 :( Please help! :( 请帮忙!

1) Create a form POJO with your attribute productRecord, resultEntity, resultCode. 1)使用属性productRecord,resultEntity,resultCode创建一个表单POJO。 Ex MyForm.java 前MyForm.java

2) Set this form in the model (in your init method) Ex : model.addAttribute("myForm", new MyForm()) 2)在模型中设置此表单(在您的init方法中)例如: model.addAttribute("myForm", new MyForm())

3) In your jsp form declaration set : <form:form commandName="myForm" > 3)在您的jsp表单声明集中: <form:form commandName="myForm" >

4) In your controller (to retrieve result) @RequestMapping(value = "/productSearch", method = RequestMethod.POST) public ModelAndView postForm(MyForm myForm, HttpServletRequest request) {...} 4)在您的控制器中(用于检索结果) @RequestMapping(value = "/productSearch", method = RequestMethod.POST) public ModelAndView postForm(MyForm myForm, HttpServletRequest request) {...}

Then you can access values using myForm.getProductRecord() for example. 然后,您可以使用例如myForm.getProductRecord()访问值。

I don't know why you'd ever want to do this.. But, Spring exposes the request as a ThreadLocal. 我不知道您为什么要这么做。但是,Spring将请求公开为ThreadLocal。 You can access it on the current thread but you can't on any spawned threads without passing it. 您可以在当前线程上访问它,但是如果不通过它就不能在任何生成的线程上访问它。

Here's what you do: 这是您的工作:

@RequestMapping("/whatever")
public ModelAndView controllerMethod() {
    HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();

// Do as you want with the request without having to pass it in
}

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

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