简体   繁体   English

如何检查JSP是否在Spring MVC中转到控制器,以及如何显示从控制器到JSP的列表?

[英]How to check if jsp went to the controller in spring mvc and how to display list from controller to jsp?

I have a spring mvc program running in tomcat. 我有一个在tomcat中运行的spring mvc程序。 It has a datatable that when a user clicks the button, certain values of the row where the button is clicked will be passed to the controller. 它具有一个数据表,当用户单击按钮时,单击按钮的行的某些值将传递给控制器​​。

It looks something like this 看起来像这样

Here is how I create my datatable. 这是我创建数据表的方式。

$(document).ready(function () {

            $('#datatables').DataTable({
                "dom": '<"toolbar">frtip',
                "responsive": true,
                "scrollY": "550px",
                "scrollCollapse": true,
                "ajax": "smsSenders.json",
                "aoColumns": [
                    {"mData": "sender"},
                    {"mData": "content"},
                    {"mData": "receiveTime"},
                    {"mData": "portId",
                        "fnCreatedCell": function (nTd,oData) {
                            $(nTd).html("<i class='ti-pencil-alt btn btn-simple btn-assign btn-icon' data-toggle='modal' data-mode='asign'></i>\n\n\
                            <i class='ti-comment-alt btn btn-simple btn-reply btn-icon' data-target='#conversationModal' data-toggle='modal' data-mode='conversation'></i>\
                         ");
                        }
                    }
                ],
                language: {
                    "search": "_INPUT_",
                    searchPlaceholder: "Search records"
                }

            });

        demo.initCirclePercentage();

Here is how I pass the certain values of the row where the button is clicked. 这是我传递单击按钮的行的某些值的方法。

$('#datatables tbody').on( 'click', '.btn-reply', function () {
            var currentRow = $(this).closest("tr");
            var data = $('#datatables').DataTable().row(currentRow).data();
            var sender = data['sender'];
            var smsc = data['smsc'];
            alert(sender + "-----------------"+smsc);
            $.ajax({
                   url:'dashboard',
                   data: {
                    "sender": sender,
                    "smsc": smsc
                   },
                   success: function(){
                       alert("success");
                   },
                   error: function(){
                       alert("error");
                   },
                   type: 'POST'
                });
            });

Here is my controller class. 这是我的控制器类。

@RequestMapping(value = {"/dashboard"}, method = RequestMethod.POST)
public String conversationList(ModelMap model, @RequestParam(value = "sender", required = true) String sender, 
        @RequestParam(value = "smsc", required = true) String smsc, HttpServletRequest request,
        @ModelAttribute("conversationList") ConversationMessages conversationMessage) throws Exception{
    LOGGER.debug("sender==================================="+sender);

    List<ConversationMessages> conversationList = dashboardService.getConversationList(sender,smsc);
    model.addAttribute("conversationList",conversationList);
    LOGGER.debug("this is conversation============================="+conversationList);

    return "dashboard";    
}

I don't know if what I'm doing here is correct. 我不知道我在这里做什么是正确的。 But it doesn't give any error in the browser console and server log. 但这不会在浏览器控制台和服务器日志中显示任何错误。

Do I need to use the @ModelAttribute in my controller? 我是否需要在控制器中使用@ModelAttribute?

Here is my xml mapper 这是我的XML映射器

<resultMap id="conversationList" type="com.beneco.cwms.domain.dashboard.ConversationMessages">
    <result property="messageId" column="message_id"/>
    <result property="sender" column="sender"/>
    <result property="smsc" column="smsc"/>        
    <result property="portId" column="port_id"/>
    <result property="content" column="content"/>
    <result property="receiveTime" column="receive_time"/>
    <result property="hasRead" column="has_read"/>
    <result property="isAssignedToTicket" column="is_assigned_to_ticket"/> 
</resultMap>

<select id="getConversationList" parameterType="map" resultMap="conversationList">

    SELECT * FROM (SELECT*FROM SMS_MESSAGES WHERE SENDER = #{sender} and SMSC = #{smsc} and IS_ASSIGNED_TO_TICKET is null 
        UNION
    SELECT * FROM SMS_REPLY WHERE SMSC = #{sender} and SENDER = #{smsc} and IS_ASSIGNED_TO_TICKET is null) order by RECEIVE_TIME;

</select>

Here is my service implementation class. 这是我的服务实现类。

public class DashboardServiceImpl implements DashboardService{

    private static final Logger LOGGER = Logger.getLogger(SmsServiceImpl.class);

    @Autowired
    private DashboardMapper dashboardmapper;

    @Override
    public List<ConversationMessages> getConversationList(String sender, String smsc) {
        LOGGER.debug(dashboardmapper.getConversationList(sender,smsc) + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        return dashboardmapper.getConversationList(sender,smsc);
    }
}

Here is how I display the list in my jsp 这是我在jsp中显示列表的方式

<div class="modal fade" id="conversationModal" role="dialog">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Conversation</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <c:forEach items="${conversationList}" var="user">

                    <div class="col-md-12">
                        <div class="form-group">
                            <label>Sender: ${sender}</label>
                            <label>Content: ${sender}</label>
                        </div>
                    </div>
                    </c:forEach>
                </div>
            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-default" data-dismiss="modal" id="saveAddress">Send</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

Is there any way for the controller to return a modal inside a page instead of the page itself? 控制器有什么方法可以在页面内部而不是页面本身返回模式信息吗?

Here's what I'm trying to do: 这是我想做的事情:

  1. The user clicks the button. 用户单击按钮。
  2. Values where the button is clicked will be passed to the controller. 单击按钮的值将传递到控制器。
  3. A query will be executed based on the values passed. 将根据传递的值执行查询。
  4. Controller will return to the same page but a modal showing the query results. 控制器将返回到同一页面,但会显示查询结果的模式。

I have this showing in my tomcat. 我的雄猫里有这个节目。

WebAccessDeniedHandler:35 handle - User: admin attempted to access the protected URL: /cwms/dashboard WebAccessDeniedHandler:35句柄-用户:管理员试图访问受保护的URL:/ cwms / dashboard

TIA! TIA!

You don't need to use @ModelAttribute since you don't pass any model, @RequestParam is enough. 您不需要使用@ModelAttribute因为您无需传递任何模型,@ @RequestParam就足够了。 As you mention the alert is working inside the ajax and the data doesn't pass to the controller, I think, you are using just absolute path in the ajax URL which is no longer available, so better you should use relative path. 正如您提到的那样,警报正在ajax内部运行并且数据不会传递给控制器​​,我认为,您在ajax URL中仅使用了绝对路径,而该绝对路径不再可用,因此最好使用相对路径。 Even Absolute path is fine, if you properly define it. 如果正确定义,即使绝对路径也可以。

The following code shows a relative path, 以下代码显示了相对路径,

<script>
   <c:url value = "/dashboard" var="dbURL"/> //hope you don't have a parent mapping like ..:8080/myProject/index/dashboard
   //other codes
   $.ajax({
   url : "${dbURL}"
   //codes
})
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM