在Spring MVC中使用jTable(jQuery插件)

时间:2019-01-10 19:11:12

标签: java jquery spring-mvc jtable jquery-jtable

我正在寻求一个我无法单独解决的错误的帮助。我正在建立一个使用Spring MVC和jTable(jTable.org)的项目。我收到一个错误消息:无法加载资源:每当用户尝试访问页面时,服务器都会以404(未找到)状态进行响应。 (1.png)。在日志中,我尝试检索request.getParameter(“ action”),并且它返回null。

这是我的jsp:

<%@ include file="/WEB-INF/jsp/header.jsp"%>

<div class="container-fluid col-md-8 col-md-offset-2 content">
        <h2>Component Management</h2>
        <div id="ComponentManagementContainer"></div>
</div>

<%@ include file="/WEB-INF/jsp/footer.jsp"%>

这是我的jTable实现,仿照jTable Demo中的演示:

$(document).ready(function () {
    $('#ComponentManagementContainer').jtable({
        title: '<center> DETAILS DASHBOARD </center>',
        actions: {
            listAction: 'componentMgmt?action=list'
        },
        fields: {
            componentId: {
                title: 'Component ID',
                list: true,
                key: true,
                edit: true,
                input: function (data) {
                    return '<input type="text" readonly class="jtable-input-readonly" name="name" value="' + data.value + '"/>';
                }
            },
            componentDescription: {
                title: 'Component Description',
                list: true,
                edit: true,
                width: '15%',
                input: function (data) {
                    return '<input type="text" readonly class="jtable-input-readonly" name="name" value="' + data.value + '"/>';
                }
            }
        }
    });
    $('#ComponentManagementContainer').jtable('load');
});

这是我的控制器类:

public class ComponentManagementController implements Controller {
    @RequestMapping(value = "/componentMgmt")
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String action = (String) request.getParameter("action");
        List < Components > components = new ArrayList < Components > ();
        ComponentServiceImpl componentServiceImpl = new ComponentServiceImpl();

        System.out.println("====================================inside test request.getParameter(action)" + " " + request.getParameter("action")); // PRINTS NULL
        if (action != null) {
            if (action.equals("list")) {
                try {
                    //Fetch Data from db
                    components = componentServiceImpl.getAllComponentValues();

                    //Convert Java Object to Json
                    Gson gson = new Gson();
                    JsonElement element = gson.toJsonTree(components, new TypeToken < List < Components >> () {}.getType());
                    JsonArray jsonArray = element.getAsJsonArray();
                    String listData = jsonArray.toString();

                    //Return Json in the format required by jTable plugin
                    listData = "{\"Result\":\"OK\",\"Records\":" + listData + "}";
                    response.setContentType("application/json");
                    response.getWriter().print(listData);
                    System.out.println(listData);
                } catch (Exception ex) {
                    String error = "{\"Result\":\"ERROR\",\"Message\":" + ex.getStackTrace() + "}";
                    response.getWriter().print(error);
                }
            }
        }
        return new ModelAndView("componentMgmt");
    }

}

servlet.xml

<?xml version="1.0" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename">
            <value>messages</value>
        </property>
    </bean>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="requestContextAttribute" value="requestContext" />
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="urlMapping"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/ComponentManagement.bi">ComponentManagementController</prop>
            </props>
        </property>
    </bean>

    <bean id="ComponentManagementController"
        class="com.webapp.controller.ComponentManagementController" />
</beans>

如何检索从jTable发送的action = list的值?我认为这是错误404背后的原因。请帮助。

提前谢谢!

0 个答案:

没有答案