我正在使用Spring MVC平台,并且遇到了问题。 我希望允许用户发送他们在平台上添加的约定和事件的邀请。我为这两个人都有model
,他们工作的很好,但是当向别人发送邀请时,我似乎无法找到动态更新<form:select>
选项列表的方式。 当一个人邀请他人参加一个约定时,他们有两个下拉列表。第一个是约定下拉列表,它包含该人员添加的所有约定。每个Convention
都有一个Event
s的列表,第二个下拉列表应显示所选约定的所有事件。 因此,对于第一个下拉列表中的每个选项,第二个下拉列表中应该有多个选项。Spring MVC更改<form:select>选项与其他动态<form:select>
我会在这里发布模型,如果它们对你来说意味着什么,并且削减与这个问题无关的代码。
事件:
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";
}
}
服务的方法名是自我解释,我只是得到一个列表用某种标准,无论是用户名或约定。
不,我不能以这种方式访问模型的变量。我必须将JSP页面上的对象实例添加为模型属性,然后才能获取变量的值。我能做这样的事情的唯一方法是声明一个var =“convention”,然后从那里获取eventName ... 但是我没有在页面上定义的约定,我有一个约定列表。 反正谢谢你。 –
在这一点上,我只需要得到“”working。显然,我不能得到convention.conventionName,因为它不在循环中,但是在那里输入文本给出了正确的结果。现在我只需要找到一种方法从选项框中获取值并将其与事件进行比较。常规名称 –
由于您要分别发送,所以无法访问来自约定的事件。我会用你的控制器编辑我的答案 –