1

我正在尝试在我的 weapp 中实现 ajax 功能,我正在使用 Spring MVC 和 Tiles,对于 Ajax 调用,我正在使用 jquery。

一切正常,除了 ajax 功能

相关代码如下:

瓷砖配置:

<beans:bean class="org.springframework.js.ajax.AjaxUrlBasedViewResolver" id="tilesViewResolver">
        <beans:property name="viewClass" value="org.springframework.js.ajax.tiles2.AjaxTilesView"/>
    </beans:bean>

<beans:bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" id="tilesConfigurer">
    <beans:property name="definitions">
        <beans:list>
            <beans:value>/WEB-INF/plantilla/plantilla.xml</beans:value>
            <!-- Scan views directory for Tiles configurations -->
            <beans:value>/WEB-INF/views/**/vistas.xml</beans:value>
        </beans:list>
    </beans:property>
</beans:bean>

我的控制器:

@RequestMapping(value = "clientes/listarProvincias", method = RequestMethod.GET,headers="Accept=*/*")
public @ResponseBody List<Provincia> listarProvincias(@RequestParam(value = "departamento", required = true)String codigoDepartamento){
    logger.info("Ingresando al Metodo Listar Provincias con Ajax");
    Integer codDep = Integer.parseInt(codigoDepartamento);
    return this.provinciaDAO.listarProvinciaPorDepartamento(codDep);
}

我的 JSP

   <script type="text/javascript">
$(document).ready(function() { 
    $('#departamento').change(
            function() {
                $.getJSON('clientes/listarProvincias', {
                    departamento : $(this).val(),
                    ajax : 'true'
                }, function(data) {
                    alert("Ingresando a JSON");
                    var html = '<option value="">Departamento</option>';
                    var len = data.length;
                    for ( var i = 0; i < len; i++) {
                        html += '<option value="' + data[i].name + '">'
                                + data[i].name + '</option>';
                    }
                    html += '</option>';

                    $('#provincias').html(html);
                });
            });
});
</script>

表格的一部分

 <p>
                        <label>Departamento</label>
                        <span class="field">
                            <form:select path="departamento.codDpto" id="departamento">
                                <form:option value="" ></form:option>
                                <c:forEach items="${departamentos}" var="departamento">
                                    <form:option value="${departamento.codDpto}">${departamento.descripcion}</form:option>
                                </c:forEach>
                            </form:select>
                            <label class="error">
                                <form:errors path="estaReg" cssClass="error"></form:errors>
                            </label>
                        </span>
                    </p>

                    <p>
                        <label>Provincia</label>
                        <span class="field">
                            <form:select path="provincia.codprovincia" id="provincias">
                                <form:option value="" ></form:option>
                                <c:forEach items="${provincias}" var="provincia">
                                    <form:option value="${provincia.codprovincia}">${provincia.descripcion}</form:option>
                                </c:forEach>
                            </form:select>
                            <label class="error">
                                <form:errors path="estaReg" cssClass="error"></form:errors>
                            </label>
                        </span>
                    </p>

Google Chomre 工具显示:

GET http://localhost:8080/sictem/clientes/listarProvincias?departamento=5&ajax=true 404 (No Encontrado)

日志

    DEBUG: org.springframework.security.web.FilterChainProxy - /clientes/listarProvincias?departamento=6&ajax=true at position 1 of 10 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
DEBUG: org.springframework.security.web.context.HttpSessionSecurityContextRepository - No HttpSession currently exists
DEBUG: org.springframework.security.web.context.HttpSessionSecurityContextRepository - No SecurityContext was available from the HttpSession: null. A new one will be created.
DEBUG: org.springframework.security.web.FilterChainProxy - /clientes/listarProvincias?departamento=6&ajax=true at position 2 of 10 in additional filter chain; firing Filter: 'LogoutFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /clientes/listarProvincias?departamento=6&ajax=true at position 3 of 10 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /clientes/listarProvincias?departamento=6&ajax=true at position 4 of 10 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /clientes/listarProvincias?departamento=6&ajax=true at position 5 of 10 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /clientes/listarProvincias?departamento=6&ajax=true at position 6 of 10 in additional filter chain; firing Filter: 'RememberMeAuthenticationFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /clientes/listarProvincias?departamento=6&ajax=true at position 7 of 10 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
DEBUG: org.springframework.security.web.authentication.AnonymousAuthenticationFilter - Populated SecurityContextHolder with anonymous token: 'org.springframework.security.authentication.AnonymousAuthenticationToken@9055e4a6: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@957e: RemoteIpAddress: 127.0.0.1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS'
DEBUG: org.springframework.security.web.FilterChainProxy - /clientes/listarProvincias?departamento=6&ajax=true at position 8 of 10 in additional filter chain; firing Filter: 'SessionManagementFilter'
DEBUG: org.springframework.security.web.session.SessionManagementFilter - Requested session ID46A28166F0557A3A00BB916481349AC8 is invalid.
DEBUG: org.springframework.security.web.FilterChainProxy - /clientes/listarProvincias?departamento=6&ajax=true at position 9 of 10 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /clientes/listarProvincias?departamento=6&ajax=true at position 10 of 10 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
DEBUG: org.springframework.security.web.util.AntPathRequestMatcher - Checking match of request : '/clientes/listarprovincias'; against '/*'
DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Public object - authentication not attempted
DEBUG: org.springframework.security.web.FilterChainProxy - /clientes/listarProvincias?departamento=6&ajax=true reached end of additional filter chain; proceeding with original chain
DEBUG: org.springframework.security.web.context.HttpSessionSecurityContextRepository - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
DEBUG: org.springframework.security.web.access.ExceptionTranslationFilter - Chain processed normally
DEBUG: org.springframework.security.web.context.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed

我不知道我做错了什么。此致,

4

1 回答 1

0

检查您是否还需要更改控制器声明

  @RequestMapping(value = "json", headers = "Accept=application/json")
    @ResponseBody
    public ResponseEntity<java.lang.String> listJson(@RequestParam String nombre) {
        HttpHeaders headers = new HttpHeaders();

        // PAY ATTENTION TO THIS:
        headers.add("Content-Type", "application/json; charset=utf-8");

        List<Medico> result = null;
        if (org.apache.commons.lang3.StringUtils.isEmpty(nombre)) {
            result = Provincia.findProvinciaEntries(0, 10);
        } else result = Provincia.findProvinciaEntries(0, 10);


        // NEED TO CREATE JSON ARRAY
        String data = "{\"items\":" + Provincia.toJsonArray(result) + " }";
        return new ResponseEntity<String>(data, headers, HttpStatus.OK);
    }

希望这对你有帮助

于 2014-02-24T20:39:31.773 回答