【问题标题】:Assign to object selected value分配给对象选定值
【发布时间】:2015-08-14 19:14:05
【问题描述】:

在 JSP 的下拉框中,我有一个国家/地区列表。我应该将选定的国家/地区分配给“组织”对象作为对象“国家/地区”,但我有错误的请求错误。

JSP,已编辑

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@include file='css/styles.css'%>


<html>
<head>
<title>TestWebApp</title>
</head>
<body>
<table align = "center" class="table" cellspacing='0'>
<tr>
<th valign = "top">Add an organization</th>
</tr>
<form method="POST">
<tr>
<td>Name:</td>
<td><input type="text" name="name" value="${name}" /></td>
</tr>
<tr>
<td>Country: </td>
<td>
<%--
<select name="country">
<c:forEach var="country" items="${countries}">
<option value="${country}">${country.name}</option>
</c:forEach>
</select>
--%>
</td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address" value="${address}" /></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="phone" value="${phone}" /></td>
</tr>
<tr>
<td>Market Cap:</td>
<td><input type="text" name="marketCap" value="${marketCap}" /></td>
</tr>
</table>
<table align = "center" class="table" cellspacing='0'>
<tr>
<td><input type="reset" /></td>
<td><input type="submit" /></td>
</table>
</form>

</body>
</html>

模型、组织:

@Entity
public class Organization {

    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    //I should assign "Country" object to this field
    @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinColumn(name="country")
    private Country country;
    private String address;
    private String phone;
    private Long market_cap;

    public Organization(Integer id, String name, Country country, String address, String phone, Long market_cap) {
        this.id = id;
        this.name = name;
        this.country = country;
        this.address = address;
        this.phone = phone;
        this.market_cap = market_cap;
    }
...// getters and setters

模特,国家:

@Entity
public class Country {
    @Id
    @GeneratedValue
    private Integer id_country;
    private String name;
    private String isocode;

    public Country() {
    }

public Country(Integer id_country, String name, String isocode) {
    this.id_country = id_country;
    this.name = name;
    this.isocode = isocode;
} ...//getters and setters

控制器

@RequestMapping(value="add", method=RequestMethod.GET)
    public ModelAndView addOrganization() {
    ModelAndView modelAndView = new ModelAndView("add");
    Organization organization = new Organization();
    modelAndView.addObject("organization", organization);
    List<Country> countries = countryService.listOfCountries();
    modelAndView.addObject("countries", countries);
    return modelAndView;
}

【问题讨论】:

  • 你没有提到你是如何触发这个错误的
  • 来自 Alexandre Picard-Lemieux 的评论:该方法的名称是什么?
  • 点击ui中的提交按钮后
  • 那么你不认为重要的是构建表单和提交方法的方式吗?我们可能不关心您发布的代码
  • 对不起,伙计们,有时我不知道什么有用。所以,也许我对你的理解不正确,但我编辑了帖子的 jsp 部分

标签: java hibernate jsp


【解决方案1】:

您正在使用POST,但需要GET。我对@RequestMapping 了解不多,但这似乎很明显那里有问题。最后,你的表单没有action,所以它几乎没有机会做任何事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 2021-12-11
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多