使用方法POST将查询字符串从视图发送到控制器

时间:2016-11-18 02:02:09

标签: java html spring jsp spring-mvc

我是春季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>

我想将firstname和lastname附加到字符串中。我该怎么做?

这是我的控制器类

@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;

}

2 个答案:

答案 0 :(得分:0)

首先你的html代码需要修改, 从?firstname={firstname}&lastname={lastname}代码的action属性中移除<form>

<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;

}

答案 1 :(得分:0)

您当前的代码几乎没有根本问题:

(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 code:

<%@ 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获取类似示例。