【问题标题】:Send Query String from view to controller using method POST使用 POST 方法将查询字符串从视图发送到控制器
【发布时间】:2017-04-01 18:45:45
【问题描述】:

我是 Spring MVC 的新手。我被要求查询字符串应该使用 POST 方法向控制器发送数据。我知道它如何与 GET 一起工作,但我不知道如何使用 POST 方法将表单值附加到查询字符串中。我正在做这样的事情,

<form action="/user/userId?firstname={firstname}&lastname={lastname}" method="POST">
Enter First Name<input type="text" name = "firstname" />
Enter Last Name<input type="text" name = "lastname" />
<input type="submit" value = "Submit" />
</form>

我想将名字和姓氏附加到字符串中。我该怎么做?

这是我的控制器类

@RequestMapping(value = "/user/userId",method = RequestMethod.POST)
public ModelAndView submitForm(@RequestParam Map<String,String> queryUser)
{
    System.out.println(queryUser.get("firstName"));
    context = new ClassPathXmlApplicationContext("beans.xml");
    Service service = (Service) context.getBean(Service.class);
    /*service.save(queryUser);*/
    ModelAndView model = new ModelAndView("SecondPage");
    return model;

}

【问题讨论】:

  • 你为什么使用Map?如果你知道你想要哪些参数,直接为它们声明@RequestParam注解参数即可。你不需要把它们放在 URL 中,这些是表单参数。
  • 为什么每次请求都初始化一个新的ApplicationContext
  • 我忘记删除 applicationcontext 对象。我的问题是如何将表单值(即名字和姓氏)附加到查询字符串?@SotiriosDelimanolis
  • 你为什么要这么做?
  • 这是给我的要求,不知道如何附加表单值。你能帮忙吗?

标签: java html spring jsp spring-mvc


【解决方案1】:

首先在你的html代码中需要修改, 从&lt;form&gt; 标签的action 属性中删除?firstname={firstname}&amp;lastname={lastname}

<form action="/user/userId" method="POST">
Enter First Name<input type="text" name = "firstname" />
Enter Last Name<input type="text" name = "lastname" />
<input type="submit" value = "Submit" />
</form>

现在您可以使用@RequestParam 获取输入值,如下所示:

@RequestMapping(value = "/user/userId", method = RequestMethod.POST)
public ModelAndView submitForm(@RequestParam("firstname") String firstname, 
                               @RequestParam("lastname") String lastname)
{
    System.out.println("firstname: "+firstname); // Prints First Name
    System.out.println("lastname: "+lastname); // Prints Last Name
    // Your service call here
    ModelAndView model = new ModelAndView("SecondPage");
    return model;

}

【讨论】:

    【解决方案2】:

    您当前的代码存在一些基本问题:

    (1)使用http POST请求参数(不推荐),而是使用@ModelAttribute如下图接收请求数据

    (2) 在控制器类中加载new ClassPathXmlApplicationContext(这只会在应用程序启动期间完成一次)。而是在启动期间使用component-scan@Autowire 加载服务bean,如下所示

    要解决这些问题,您可以参考以下内容:

    控制器类:

    @Controller
    @RequestMapping(value="/users")
    public class UserController  {
    
        @Autowired
        private UserService userService; //Autowire your User Service
    
        @RequestMapping(method=RequestMethod.GET)
        public String userInput(Model model) {
            User user = new User();
            model.addAttribute("userForm", user);
            return "User";
        }
    
        @RequestMapping(method=RequestMethod.POST)
        public String userSubmit(@ModelAttribute("userForm") User user, Model model) {
    
            userService.save(user);
            model.addAttribute("Result", User details added successfully");
            model.addAttribute("userDetails", user);
            return "UserSaveResult";
        }
    }
    

    HTML 代码:

    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    
    <form:form method="post" modelAttribute="userForm" action="/users/save">
    Enter First Name<form:input type="text" path = "firstname" />
    Enter Last Name<form:input type="text" path = "lastname" />
    <input type="submit" value = "Submit" />
    </form:form>    
    

    您可以参考here 获取类似示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 2014-12-06
      • 2019-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多