简体   繁体   English

AJAX没有打Spring MVC控制器

[英]ajax is not hitting spring mvc controller

I am new in spring mvc and hibernate and jquery. 我是春季MVC和Hibernate和Jquery的新手。 When I call one of function in spring mvc controller using ajax call it is not reaching to that controller function and also when i inspected in chrome console it throws error on every call-'POST http://localhost:8080/springmvc/GetAllUserName 403 (Forbidden)' I am also using spring security with csrf. 当我使用Ajax调用spring mvc控制器中的功能之一时,它无法到达该控制器功能,并且当我在chrome控制台中进行检查时,它在每次调用时都会引发错误-'POST http:// localhost:8080 / springmvc / GetAllUserName 403 (禁止)'我也在csrf中使用spring security。 Below is my admin.jsp 以下是我的admin.jsp

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page session="true"%>
<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
    <script>
        function formSubmit() {
            document.getElementById("logoutForm").submit();
        }
        function getAllUserName(){
            var name = $("#name").val();
            var trimmedName = name.trim();
            if(trimmedName!=null){
                $.ajax({
                    type: "POST",
                    url: "GetAllUserName",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify({userName:trimmedName}),
                    dataType: "json",
                    success: function (returnedList) {
                        alert("response");
                    },
                });
            }
        }
    </script>
</head>
<body>

    <c:if test="${pageContext.request.userPrincipal.name != null}">
        <h4 align="right">
            <%-- Welcome : ${pageContext.request.userPrincipal.name} |  --%><a href="javascript:formSubmit()"> Logout</a>
        </h4>
    </c:if>
    Enter User Name: <input type="text" id="name" onkeyup="getAllUserName()" />
    <div id="showList"></div>

    <c:url value="/j_spring_security_logout" var="logoutUrl" />
    <!-- csrt for log out-->
    <form action="${logoutUrl}" method="post" id="logoutForm">
      <input type="hidden" 
        name="${_csrf.parameterName}"
        value="${_csrf.token}" />
    </form>
</body>
</html>

LoginController.java LoginController.java

package com.csc.springmvc.web;

import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {
    @RequestMapping(value = { "/**" }, method = RequestMethod.GET)
    public ModelAndView welcomePage() {

        return new ModelAndView("redirect:/admin");
    }

    @RequestMapping(value = "/admin", method = RequestMethod.GET)
    public ModelAndView adminPage() {

        ModelAndView model = new ModelAndView();
        model.setViewName("admin");

        return model;

    }

    //Spring Security see this :
        @RequestMapping(value = "/login", method = RequestMethod.GET)
        public ModelAndView login(
            @RequestParam(value = "error", required = false) String error,
            @RequestParam(value = "logout", required = false) String logout) {

            ModelAndView model = new ModelAndView();
            if (error != null) {
                model.addObject("error", "Invalid username and password!");
            }

            if (logout != null) {
                model.addObject("msg", "You've been logged out successfully.");
            }
            model.setViewName("login");

            return model;

        }

    @RequestMapping(value = "/GetAllUserName", method = RequestMethod.POST)
    @ResponseBody
    public String registerNewUser(@RequestBody User user){
        System.out.println(user.getUsername());
        return "Hello";
    }
}

spring-basic-context.xml spring-basic-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <mvc:resources mapping="/images/**" location="/resources/images/" />
    <mvc:resources mapping="/JSPresources/**" location="/JSPresources/" />

    <context:component-scan base-package="com.csc" />
    <mvc:annotation-driven/>
    <context:annotation-config/> 
</beans:beans>

springmvc-servlet.xml springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/security 
       http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

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


</beans>

Can someone please help me in this problem. 有人可以帮我解决这个问题。

Your web.xml servlet mapping url-pattern might be too restrictive, causing the Spring Dispatcher Servlet to never get reached. 您的web.xml Servlet映射url-pattern可能过于严格,导致无法访问Spring Dispatcher Servlet。 Try: 尝试:

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

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

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