带 html 复选框的 spring 表单控制器

发布于 2024-10-27 01:57:21 字数 5038 浏览 1 评论 0原文

我正在尝试使用弹簧表单控制器绑定输入类型复选框,但我失败了。

这里我发布了控制器、bean 和 jsp 示例,还有一件事是我无法使用 。

下面是代码:

控制器:

package com.test.web;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.vaannila.domain.User;
import com.vaannila.service.UserService;

@SuppressWarnings("deprecation")
public class UserController extends SimpleFormController {

    private UserService userService;

    public UserController() {
        setCommandClass(User.class);
        setCommandName("user");
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    protected ModelAndView onSubmit(Object command) throws Exception {
        User user = (User) command;
        user.setCommunity(user.getCommunity());
        userService.add(user);
        return new ModelAndView("userForm","user",user);
    }

}

jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Page</title>
<script>
function submitForm(){
document.testForm.submit();

}
</script>
</head>
<body>

<form:form method="POST" commandName="user" name="testForm" action="./userRegistration.htm">
    <table>
        <tr>
            <td>User Name :</td>
            <td><form:input path="name" /></td>
        </tr>
        <tr>
            <td>Password :</td>
            <td><form:password path="password" /></td>
        </tr>
        <tr>
            <td>Gender :</td>
            <td><form:radiobutton path="gender" value="M" label="M" /> 
                <form:radiobutton path="gender" value="F" label="F" /></td>
        </tr>
        <tr>
            <td>Country :</td>
            <td><form:select path="country">
                <form:option value="0" label="Select" />
                <form:option value="1" label="India" />
                <form:option value="2" label="USA" />
                <form:option value="3" label="UK" />
            </form:select></td>
        </tr>
        <tr>
            <td>About you :</td>
            <td><form:textarea path="aboutYou" /></td>
        </tr>
        <tr>
            <td>Community :</td>

                <td><input type="checkbox" name="community" value="Hibernate"/>Hibernate</br>
                <input type="checkbox" name="community" value="test"/>test</br>
                <input type="checkbox" name="community" value="test1"/>test1</br>
                </td>
        </tr>
        <tr>
            <td></td>
            <td><form:checkbox path="mailingList"
                label="Would you like to join our mailinglist?" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" onclick="submitForm();"></td>
        </tr>
    </table>

</form:form>

</body>
</html>

Java beans:

package com.test.domain;

public class User {

    private String name;
    private String password;
    private String gender;
    private String country;
    private String aboutYou;
    private String[] community;
    private Boolean mailingList;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getAboutYou() {
        return aboutYou;
    }
    public void setAboutYou(String aboutYou) {
        this.aboutYou = aboutYou;
    }
    public String[] getCommunity() {
        return community;
    }
    public void setCommunity(String[] community) {
        this.community = community;
    }
    public Boolean getMailingList() {
        return mailingList;
    }
    public void setMailingList(Boolean mailingList) {
        this.mailingList = mailingList;
    }


}

我尝试了不同的方法,但没有运气。请提供任何提示。

I am trying to bind the input type checkbox using spring form controller,But i failed .

Here i am posting Controller,bean and jsp example,One more thing is i can't use
.

Below is the code:

Controller:

package com.test.web;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.vaannila.domain.User;
import com.vaannila.service.UserService;

@SuppressWarnings("deprecation")
public class UserController extends SimpleFormController {

    private UserService userService;

    public UserController() {
        setCommandClass(User.class);
        setCommandName("user");
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    protected ModelAndView onSubmit(Object command) throws Exception {
        User user = (User) command;
        user.setCommunity(user.getCommunity());
        userService.add(user);
        return new ModelAndView("userForm","user",user);
    }

}

jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Page</title>
<script>
function submitForm(){
document.testForm.submit();

}
</script>
</head>
<body>

<form:form method="POST" commandName="user" name="testForm" action="./userRegistration.htm">
    <table>
        <tr>
            <td>User Name :</td>
            <td><form:input path="name" /></td>
        </tr>
        <tr>
            <td>Password :</td>
            <td><form:password path="password" /></td>
        </tr>
        <tr>
            <td>Gender :</td>
            <td><form:radiobutton path="gender" value="M" label="M" /> 
                <form:radiobutton path="gender" value="F" label="F" /></td>
        </tr>
        <tr>
            <td>Country :</td>
            <td><form:select path="country">
                <form:option value="0" label="Select" />
                <form:option value="1" label="India" />
                <form:option value="2" label="USA" />
                <form:option value="3" label="UK" />
            </form:select></td>
        </tr>
        <tr>
            <td>About you :</td>
            <td><form:textarea path="aboutYou" /></td>
        </tr>
        <tr>
            <td>Community :</td>

                <td><input type="checkbox" name="community" value="Hibernate"/>Hibernate</br>
                <input type="checkbox" name="community" value="test"/>test</br>
                <input type="checkbox" name="community" value="test1"/>test1</br>
                </td>
        </tr>
        <tr>
            <td></td>
            <td><form:checkbox path="mailingList"
                label="Would you like to join our mailinglist?" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" onclick="submitForm();"></td>
        </tr>
    </table>

</form:form>

</body>
</html>

Java beans:

package com.test.domain;

public class User {

    private String name;
    private String password;
    private String gender;
    private String country;
    private String aboutYou;
    private String[] community;
    private Boolean mailingList;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getAboutYou() {
        return aboutYou;
    }
    public void setAboutYou(String aboutYou) {
        this.aboutYou = aboutYou;
    }
    public String[] getCommunity() {
        return community;
    }
    public void setCommunity(String[] community) {
        this.community = community;
    }
    public Boolean getMailingList() {
        return mailingList;
    }
    public void setMailingList(Boolean mailingList) {
        this.mailingList = mailingList;
    }


}

I tried different ways,but no luck.Any hints please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻风 2024-11-03 01:57:21

如果未选中该复选框,浏览器将不会发送请求中的字段。该值要么为“true”,要么不发送。你永远不会得到“假”值。

为每个复选框添加一个带有 _name 的隐藏字段

EX:

然后,spring 会处理它。

the browser will not send the field in the request if the checkbox isn't checked. the value will either be "true" or not sent. you will never get a "false" value.

add a hidden field with _name for every checkbox

EX:
<input type="checkbox" name="community" value="Hibernate"/>
<input type="hidden" name="_community" value="on"/>

Then, spring will take care of it.

失与倦" 2024-11-03 01:57:21

如果您不使用表单标签,它不会自动绑定您的复选框。如果您使用纯 html,则必须绑定您的 self。

您可以通过添加社区对象列表然后使用 form:checkboxes 来解决此问题。
例如:

<form:checkboxes path="communityList" items="${communityList}" itemValue="key" itemLabel="value" />

我还建议您在使用 ModelAndView 时使用 HashMap,如下所示:

Map<String, Object> model = new HashMap<String, Object>();
model.put("user", user);
model.put("communityList", communityList);
return new ModelAndView("userFormat", model);

使用 'ServletRequestUtils' 手动绑定... http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/bind/ServletRequestUtils。 html

示例

public ModelAndView test(HttpServletRequest request, HttpServletResponse response) throws ServletRequestBindingException {
Long subscriptionOwnerId = ServletRequestUtils.getLongParameter(request, "id");
return new ModelAndView('test'); }`    

If you do not use the form tag it will not automaticly bind your checkboxes. If you use plain html you have to bind the your self.

You can solve this by adding a list of community objects and then use form:checkboxes.
For example:

<form:checkboxes path="communityList" items="${communityList}" itemValue="key" itemLabel="value" />

I would also recomend you to use a HashMap when using ModelAndView like this:

Map<String, Object> model = new HashMap<String, Object>();
model.put("user", user);
model.put("communityList", communityList);
return new ModelAndView("userFormat", model);

Manually bind using 'ServletRequestUtils'... http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/bind/ServletRequestUtils.html

Example

public ModelAndView test(HttpServletRequest request, HttpServletResponse response) throws ServletRequestBindingException {
Long subscriptionOwnerId = ServletRequestUtils.getLongParameter(request, "id");
return new ModelAndView('test'); }`    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文