2015-11-06 47 views
0

我是新的春天mvc和休眠和jQuery的。當我調用spring的mvc控制器中的一個函數使用ajax調用時,它沒有達到該控制器的功能,並且當我在Chrome控制檯中檢查時,它會在每次調用時拋出錯誤 - 'POST http://localhost:8080/springmvc/GetAllUserName 403(Forbidden)'我也使用spring security與csrf。 下面是我admin.jsp阿賈克斯是不是彈簧mvc控制器

<%@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

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"; 
    } 
} 

彈簧基本-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中

<?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> 

可以一些請幫助我解決這個問題。

+0

請僅顯示理解問題所需的miminum代碼。這太長了! – JotaBe

+0

現在我已經刪除了pom.xml和web.xml – user2409128

回答

1

您的web.xml servlet映射url-pattern可能過於嚴格,從而導致Spring Dispatcher Servlet無法到達。請嘗試:

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

在瀏覽器上執行此操作時出現錯誤。 '這個網頁已重定向循環' – user2409128

+0

這完全與您的處理程序編寫的方式有關:@RequestMapping(value = {「/ **」},method = RequestMethod.GET) public ModelAndView welcomePage(){ return新的ModelAndView(「重定向:/ admin」); },但至少是現在到達Dispatcher Servlet的請求。 –