Spring MVC使用其他<form:select>

时间:2017-07-19 23:20:03

标签: java spring forms spring-mvc model-view-controller

我正在使用Spring MVC平台,我遇到了一个问题。 我希望允许用户将邀请发送到他们在平台上添加的约定和事件。我有两个model,但它们运行正常,但在向某人发送邀请时,我似乎无法找到动态更新<form:select>选项列表的方法。 当一个人邀请另一个人参加大会时,他们会有两个下拉列表。第一个是约定下拉列表,它包含该人添加的所有约定。每个Convention都有一个Event列表,第二个下拉列表应该显示所选约定的所有事件。 因此,对于第一个下拉列表中的每个选项,第二个下拉列表中应该有多个选项。

我会在这里发布模型,如果它们对您有意义,并删除与此问题无关的代码。

事件:

 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";
    }

}

服务的方法名称是不言自明的,我只是得到一个带有某种标准的列表,无论是用户名还是惯例。

1 个答案:

答案 0 :(得分:0)

在您的实体中,您已声明了一个事件列表,然后您可以访问您的事件,例如convetionList.eventName

<div class="form-group">
    <label for="eventName">Event: </label>
    <form:select path="conName" name="conName" items="${conventionList.eventName}"/>
</div>

[编辑]

尝试修改控制器的响应:

@RequestMapping("/sendInvite/{sendingTo}")
public String sendInvite(@PathVariable("sendingTo") String sendingTo, Model model, Principal principal) {


    List<Convention> conventionList = conventionService.getConventionsByUsername(principal.getName());
    List<Event> eventList = eventService.getEventListForOwner(principal.getName());

    for(Covention convention : conventionList){

        for(Event event : eventList){

            if(convention.getConventionId() == event.getConventionId())
            {
                convention.conventionEvents.add(event);          

            }
        }


    }
    //Now you have all of your events associated to your conventions
    model.addAttribute("conventionList", conventionList);
    model.addAttribute("invite", new Invite());

    return "sendInvite";
}

现在,您应该可以使用${conventionList.conventionName}来获取下拉视图和${conventionList.conventionsEvents.eventName}中的所有约定,并且此对象应按惯例以动态显示

如果您需要更多帮助,请告诉我。 的问候,