你好我有2个实体
主要实体
@Entity
@Table(name = "Events")
public class Event {
//Some fields ...
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "event_eventtypes" ,
joinColumns = @JoinColumn(name = "event_id"),
inverseJoinColumns = @JoinColumn(name = "event_type_id"))
private Set<EventType> eventTypes;
//Getters and setters...
现在我有一个使用spring form-taglib
创建的表单<form:form modelAttribute="event" method="post" action="/event/registerEvent" commandName="event">
<form:label path="name">Event display name</form:label>
<form:input path="name" type="text" cssClass="form-control" placeholder="Display name"/>
<form:label path="description" >Description</form:label>
<form:textarea path="description" cssClass="form-control"/>
<form:label path="priv" cssClass="">Make private? <span style="font-family:'Open Sans', sans-serif; font-size:11px; color: dodgerblue;">(It will be seen by your friends and people who you send invitation.)</span></form:label>
<form:checkbox path="priv" cssClass="form-check-input"/>
<form:label path="age">Age limit</form:label>
<form:select path="age">
<form:options items="${age}"/>
</form:select>
<form:hidden path="lng" id="formLang" />
<form:hidden path="lat" id="formLat"/>
<%--Question appear here--%>
<form:select path="eventTypes" items="${eventTypes}" multiple="true"/>
<input type="submit" value="Submit">
控制器
@Controller
public class EventController {
private static final Logger logger = LoggerFactory.getLogger(EventController.class);
@Autowired
private EventService eventService;
@Autowired
private UserService userService;
@Autowired
private EventTypeService eventTypeService;
@RequestMapping(path = "/event/create", method = RequestMethod.GET)
public String init(ModelMap modelMap) {
List<String> tags = eventTypeService.listEventTypes().stream().map(EventType::getName).collect(Collectors.toList());
ArrayList<Integer> ageArr = new ArrayList();
ageArr.add(0);
ageArr.add(6);
ageArr.add(12);
ageArr.add(16);
ageArr.add(18);
modelMap.addAttribute("event", new Event());
modelMap.addAttribute("age", ageArr);
modelMap.addAttribute("eventTypes", tags);
return "/event/create";
}
@RequestMapping(path = "/event/registerEvent", method = RequestMethod.POST)
public String createEvent(@ModelAttribute("event") Event event, @ModelAttribute("eventTypes1") List<String> eventTypes){
event.setDate(new Date());
event.setUser(userService.getUserByUsername(
AuthenticationService.getLoggedInUser())
);
eventService.addEvent(event);
return "redirect:/";
}
完成包含值的表单后,单击“提交”,“获取错误”
400客户端发送的请求在语法上是不正确的。
因为eventTypes属性是String类型。如何将该列表作为控制器中的另一个参数发送,或者我应该怎么做?
答案 0 :(得分:0)
也许你需要一个DTO来包含事件和事件类型列表。
答案 1 :(得分:0)
当我这样做时,我发送一个DTO,其中包含事件类型的ID /字符串列表到表单,然后从DTO创建一个新事件。
对于select语句,循环遍历DTO中返回的每个选项,并使用服务findById检索每个eventType,然后将其添加到事件模型中。
for (String tag : eventDTO.getEventTags()) {
EventType eventTag = eventTypeService.findById(Long.valueOf(tag));
if (eventTag != null) {
event.getEventTypes().add(eventTag );
}
}
答案 2 :(得分:0)
找到了解酒。 只需将基于jsp的表单实现为:
<form:form modelAttribute="event" method="post" action="/event/registerEvent" commandName="event">
...
<%--There I'm not using more jsp tabs.--%>
<select multiple name="et" id="tags">
<c:forEach items="${eventTypes}" var="e">
<option value=${e}>${e}</option>
</c:forEach>
</select>
<input type="submit" value="Submit">
</form:form>
在Controller中我获取参数request.getParameterValues("et"))
- 从HttpServletRequest返回String[]