如何将文本字段值从jsp传递给spring控制器类,反之亦然

时间:2016-10-10 11:29:35

标签: javascript java jsp spring-mvc

我们如何在java类中获取jsp数据(即textarea输入)(即具有返回ModelAndView对象的函数的spring控制器?)

我们有spring控制器,因此我们无法使用servlet,因此我们不希望在doGetdoPost中获取值。

我已阅读此How to pass value or param from jsp to Spring controller帖子;但是,它没有提供任何简单的样本或有用的链接。

3 个答案:

答案 0 :(得分:1)

@RequestMapping(value="/url you are hitting",method=RequestMethod.POST)
public ModelAndView
FitchService(@ModelAttribute Bean/Model cmd,BindingResult
result,ModelMap model) {

    ModelAndView modelAndView = new ModelAndView(displayURL);
    //here displayURL is where you want to forward which is a string i.e jsp return  modelAndView 
    return modelAndView;
}

然后在jsp中获取它们:

> <c:out value="${value you want to fetch}" />

If you want to pass parameters from jsp then you can use @RequestParam annotation also.

http://jeromejaglale.com/doc/java/spring/mvc

Please refer above link

答案 1 :(得分:0)

如果您要求这样的话,请检查一下 -

https://www.mkyong.com/spring-mvc/spring-mvc-textbox-example/

答案 2 :(得分:0)

您可以构建弹簧形式并绑定模型

<%@ 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="/userActionUrl">
        <form:input path="name" type="text" /> <!-- bind to user.name-->
        <form:errors path="name" />
    </form:form>

然后在控制器中,您将收到填充的数据

@Controller
public class UserController{

@RequestMapping(value = "/userActionUrl", method = RequestMethod.POST)
public String saveOrUpdateUser(@ModelAttribute("userForm") User user,
        BindingResult result, Model model) {
    user.getName()
}


}

public class User {


    // form:input - textbox
    String name;

   //getter setters

}