【发布时间】:2017-12-25 10:27:48
【问题描述】:
我在 Spring MVC 平台上工作,遇到了问题。
我希望允许用户向他们在平台上添加的会议和活动发送邀请。我对这两个都有一个model,它们工作正常,但是当向某人发送邀请时,我似乎无法找到动态更新<form:select> 选项列表的方法。
当一个人邀请另一个人参加会议时,他们有两个下拉列表。第一个是约定下拉列表,它包含该人添加的所有约定。每个Convention 都有一个Events 列表,第二个下拉列表应显示所选约定的所有事件。
所以对于第一个下拉列表中的每个选项,第二个下拉列表中应该有多个选项。
如果模型对您有意义,我将在此处发布模型,并删除与此问题无关的代码。
事件:
public class Event {
@Id
@GeneratedValue
private int eventId;
private int conventionId;
private String conventionName;
private String eventName;
private String eventDescription;
private String websiteLink, facebookLink, twitterLink, instagramLink, youtubeLink;
private String eventType;
private String eventDateFrom;
private String eventDateTo;
private String hostedBy;
private String eventAddress;
private String eventPr;
//getters and setters
约定:
@Entity
public class Convention {
@Id
@GeneratedValue
private int conventionId;
private String conventionName;
private String conventionDescription;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Event> conventionEvents;
private String websiteLink, facebookLink, twitterLink, instagramLink, youtubeLink;
private String conventionType;
private String conPr;
private String hostedBy;
private boolean isAccepted;
//getters and setters
邀请:
@Entity
public class Invite {
@Id
@GeneratedValue
private int inviteId;
private String senderName;
private int senderId;
@NotEmpty(message = "Convention name must not be empty!")
private String conName;
private String eventName;
@NotEmpty(message = "Message must not be empty!")
private String message;
private String tripType;
private String dateFrom;
private String dateTo;
private boolean isRead;
private String sendingTo;
sendInvite.jsp [编辑 - 更改代码]
<%@ include file="/WEB-INF/views/template/header.jsp" %>
<form:form action="${pageContext.request.contextPath}/sendInvite/{${sendingTo}}" method="post" commandName="invite">
<form:hidden path="senderName" value="${pageContext.request.userPrincipal.name}"/>
<form:hidden path="sendingTo" value="${sendingTo}"/>
<div class="form-group">
<label for="eventName">Convention: </label>
</div>
<form:select path="conName" name="conName">
<c:forEach items="${conventionList}" var="convention">
<form:option value="${convention.conventionName}">${convention.conventionName}</form:option>
</c:forEach>
</form:select>
<form:select path="eventName">
<c:forEach items="${eventList}" var="event">
<c:if test="${event.conventionName == convention.conventionName}">
<form:option value="${event.eventName}">${event.eventName}</form:option>
</c:if>
</c:forEach>
</form:select>
//HERE I JUST NEED TO FIND A WAY TO GET " <c:if test="${event.conventionName == convention.conventionName}">" WORKING SINCE I CAN'T GET THE ACTUAL VALUE OF THE CONVENTION NAME.//
<div class="form-group">
<label for="message">Message body</label>
<form:textarea path="message" id="message" class="form-Control"/>
</div>
<div class="form-group">
<label for="tripType">Trip type</label>
<form:select path="tripType" name="tripType">
<form:option value="Paid">Paid</form:option>
<form:option value="Unpaid">Unpaid</form:option>
<form:option value="To be agreed">To be agreed</form:option>
</form:select>
</div>
<br><br>
<input type="submit" value="Send invitation" class="btn btn-default">
</form:form>
</body>
</html>
提前谢谢你。
[编辑 - 添加 InviteController]
@Controller
public class InviteController {
@Autowired
InviteService inviteService;
@Autowired
ProfileService profileService;
@Autowired
ConventionService conventionService;
@Autowired
EventService eventService;
@RequestMapping("/sendInvite/{sendingTo}")
public String sendInvite(@PathVariable("sendingTo") String sendingTo, Model model, Principal principal) {
Invite invite = new Invite();
List<Convention> conventionList = conventionService.getConventionsByUsername(principal.getName());
List<Event> eventList = eventService.getEventListForOwner(principal.getName());
model.addAttribute("eventList", eventList);
model.addAttribute("conventionList", conventionList);
model.addAttribute("invite", invite);
return "sendInvite";
}
@RequestMapping(value = "/sendInvite/{sendingTo}", method = RequestMethod.POST)
public String sendInvitePost(@ModelAttribute("invite") Invite invite, @PathVariable("sendingTo") String sendingTo, Model model) {
model.addAttribute("sendingTo", sendingTo);
inviteService.addInvite(invite);
return "sendInviteSuccess";
}
}
服务的方法名称是不言自明的,我只是得到一个带有某种标准的列表,无论是用户名还是约定。
【问题讨论】:
标签: java spring forms spring-mvc model-view-controller