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

时间:2022-09-19 11:49:17

How can we get jsp data (i.e. a textarea input) in a java class (i.e. spring controller with a function which return a ModelAndView object)?

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

We have spring controller so we could not use servlet and thus we do not want to get values in doGet, doPost.

我们有spring控制器所以我们不能使用servlet,因此我们不想在doGet,doPost中获取值。

I have read this How to pass value or param from jsp to Spring controller post; however, it does not provide any simple sample or helpful links.

我已经读过如何将值或参数从jsp传递给Spring控制器帖子;但是,它不提供任何简单的示例或有用的链接。

3 个解决方案

#1


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

then get them in the jsp like:

然后把它们放在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

#2


0  

Check this out, if you are asking for something like this -

看看这个,如果你要求这样的东西 -

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

#3


0  

You can construct spring form and bind a model

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

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

then in controller you receive the data populated

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

@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

}

#1


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

then get them in the jsp like:

然后把它们放在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

#2


0  

Check this out, if you are asking for something like this -

看看这个,如果你要求这样的东西 -

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

#3


0  

You can construct spring form and bind a model

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

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

then in controller you receive the data populated

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

@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

}