【问题标题】:MVC Spring Controller, display dbData in HTML fileMVC Spring Controller,在 HTML 文件中显示 dbData
【发布时间】:2018-06-13 08:34:22
【问题描述】:

如果我编写一个 Spring MVC 控制器,我如何在我的 HTML 文件中显示 DBData。

例如我有一个名为 Setting 的 db.table,我想在我的 HTML 文件中将该表中的 ID 显示为下拉列表。

 @Controller
public class CustomChannelController {

    private Setting setting;
    private Diagram diagram;
    private Channel channel;

    @RequestMapping(value = "/customchannel", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public @ResponseBody Setting getId() {
        return this.setting;
    }

<table>
    <tr>
        <td>
            <label class="col-md-12 control-label"> <%=language['UI.reportSetting.channel']%> : </label>
        </td>
        <td>
            <select id="selectReportChannel" class="form-control">
                <option value="0" ><%=setting[ID]%></option>
            </select>
        </td>
    </tr>
</table>

【问题讨论】:

    标签: java html database spring model-view-controller


    【解决方案1】:

    这取决于您如何检索数据...您使用 AJAX 调用吗?还是在页面加载时需要它们?

    让我们看看这 2 个场景 注意:在这两种情况下,我们都假设您返回的对象列表如下:

    public class Option{
      private String value;
      private String text;
      //getter and setter
    }
    

    AJAX CALL:为此我假设我们使用的是 JQuery;在这种情况下,您的 JS 或 JSP 文件中将包含如下内容:

    $.ajax({
        url : 'customchannel',
        dataType : 'json',
        contentType : 'application/json; charset=UTF-8',
        type : 'GET',
    
        success : function(items) {
            $.each(items, function (i, item) {
                $('#selectReportChannel').append($('<option>', { 
                    value: item.value,
                    text : item.text 
             }));
            });
        },
        error : function(data) {
    
        }
    });
    
    
    <select id="selectReportChannel" class="form-control">
    </select>
    

    页面加载在这种情况下,在您的控制器中,您将呈现 HTML 页面,您可以执行以下操作:

     @Controller
    public class CustomChannelController {
    
        private Setting setting;
        private Diagram diagram;
        private Channel channel;
    
        @RequestMapping(value = "/customchannel", method = RequestMethod.GET)
        public ModelAndView getId(Model uiModel) {
        uiModel.add("options", this.setting);
            return new ModelAndView("yourPage", uiModel) ;
        }
    

    那么在HTML页面中就可以这样使用JSTL了:

    <table>
        <tr>
            <td>
                <label class="col-md-12 control-label"> <%=language['UI.reportSetting.channel']%> : </label>
            </td>
            <td>
                <select id="selectReportChannel" class="form-control">
                    <c:forEach items="${options}" var="option">
                <option value="${option.value}">${option.text}</option>
            </c:forEach>
                </select>
            </td>
        </tr>
    </table>
    

    【讨论】:

      【解决方案2】:

      好的,我找到了解决方案。首先需要创建一个 Spring MVC 控制器,然后是一个服务,然后将控制器链接到 Backbone 模型文件。

      【讨论】:

        猜你喜欢
        • 2013-06-08
        • 2012-02-18
        • 1970-01-01
        • 1970-01-01
        • 2017-10-25
        • 1970-01-01
        • 2017-03-02
        • 2023-03-31
        • 1970-01-01
        相关资源
        最近更新 更多