【问题标题】:Spring MVC change <form:select> options dynamically with other <form:select>Spring MVC 使用其他 <form:select> 动态更改 <form:select> 选项
【发布时间】:2017-12-25 10:27:48
【问题描述】:

我在 Spring MVC 平台上工作,遇到了问题。 我希望允许用户向他们在平台上添加的会议和活动发送邀请。我对这两个都有一个model,它们工作正常,但是当向某人发送邀请时,我似乎无法找到动态更新&lt;form:select&gt; 选项列表的方法。 当一个人邀请另一个人参加会议时,他们有两个下拉列表。第一个是约定下拉列表,它包含该人添加的所有约定。每个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


    【解决方案1】:

    在您的实体中,您已经声明了一个事件列表,然后您可以像这样访问您的事件 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} 中的所有约定,并且该对象应该按照约定动态显示

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

    【讨论】:

    • 不,我不能那样访问模型的变量。我必须在 JSP 页面上添加一个对象的实例作为模型属性,然后才能获取变量的值。我能做这样的事情的唯一方法是声明一个 var="convention" 然后从那里获取 eventName ...但是我在页面上没有明确的约定,我有一个约定列表。还是谢谢你。
    • 此时,我只需要让“”工作。显然我无法得到convention.conventionName,因为它不在 循环中,但是在其中输入文本给出了正确的结果。现在我只需要找到一种从选项框中获取值并将其与 event.conventionName 进行比较的方法
    • 您无法访问您的约定中的事件,因为您分别发送两者。我会用你的控制器编辑我的答案
    • 我刚刚尝试了您编辑的控制器和 jsp,但在第 20 行出现错误: java.lang.NumberFormatException: For input string: "eventName"... 这是因为它是一个对象并且不是应该传递到数据库的字符串。还有 eventName 自动完成一个约定变量,这意味着该列表实际上有约定而不是事件,这没有意义,因为您的代码似乎正确......有一些错误我必须修复,就像我不允许在控制器中使用 .equals 一样。我不确定为什么会这样……
    • 在选项值中放入您的 eventId,然后在您的选项标签之间使用 eventName
    猜你喜欢
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多