2016-11-18 55 views
-1

我在Spring MVC中很新。我已经被要求查询字符串应该使用POST方法将数据发送给控制器。我知道它如何与GET协同工作,但我不知道如何使用POST方法将表单值附加到查询字符串中。我正在做这样的事情,使用方法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; 

} 
+0

你为什么使用'Map'?如果你知道你想要的参数,只需直接为它们声明'@ RequestParam'注解参数。您不需要将它们放入URL中,这些是表单参数。 –

+0

为什么你要为每个请求初始化一个新的'ApplicationContext'? –

+0

我忘了删除applicationcontext对象。 我的问题是如何将表单值附加到查询字符串的名字和姓氏?@SotiriosDelimanolis – chan

回答

0

首先在你的HTML代码的需要进行修改, 从<form>标签的action属性中删除?firstname={firstname}&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; 

} 
0

有与您当前的代码几个基本问​​题:

(1),使用HTTP POST请求的参数(不推荐),而是使用如下所示的@ModelAttribute来接收请求数据

(2)加载new ClassPathXmlApplicationContext里面的控制oller类(这将在应用程序启动期间仅执行一次)。而使用component-scan@Autowire它们加载启动期间服务豆如下所示

要解决这些问题,可以参考下面的:

控制器类:

@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="https://stackoverflow.com/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

相关问题