2011-05-25 39 views
0

嘿,我完全和ajax uri的工作方式混淆了。请看下面的代码:ajax jquery url specification

<script> 
function clickHandler(){ 
    $.ajax({ 
     **url : "http://localhost:8080/csp/ajaxchangerole.html?user=" + document.getElementById('userid').value,** 
     dataType: 'text', 
     success : function (data) { 
     $("#roleval").html(data); 
     } 
     }); 
} 

如果我更改URL,以这种方式它也可以甚至

<script> 
function clickHandler(){ 
    $.ajax({ 
     **url : "ajaxchangerole.html?user=" + document.getElementById('userid').value,** 
     dataType: 'text', 
     success : function (data) { 
     $("#roleval").html(data); 
     } 
     }); 
} 

但如果我更改URL,以任这两个网址在下面提到,它甚至不叫控制器。我不知道为什么。有人可以解释我需要在以下指定的URL上调用控制器吗?

地址: “?管理员/用户ajaxchangerole.html =” +的document.getElementById( '用户ID')值, OR 地址: “http://localhost:8080/csp/admin/ajaxchangerole.html?user=” +的document.getElementById( '用户ID')值,

这里是我的xml文件,我的片段,具有对上面这些URL映射:

<bean name="/admin/changerole.html" class="csp.spring.controller.admin.ChangeRoleFormController" > 
    <property name="customerDao"  ref="customerDao" /> 
    <property name="commandName"  value="customer" /> 
    <property name="commandClass" value="csp.model.Customer" /> 
    <property name="formView"  value="admin/changerole" /> 
    <property name="successView"  value="home" /> 
</bean> 

<bean name="/admin/ajaxchangerole.html" 
    class="csp.spring.controller.admin.ChangeRoleAjaxController"> 
    <property name="customerDao" ref="customerDao" /> 
    <property name="authorityDao" ref="authorityDao" /> 
</bean> 

我不明白为什么我必须从上述两个映射删除“/ admin”的到让这个ajax部分工作。任何帮助是极大的赞赏。提前致谢。

问候, 血清素大通

据透露, 我的控制器:

1.ChangeRoleAjaxController

 
package csp.spring.controller; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.Controller; 

import csp.model.Authority; 
import csp.model.Customer; 
import csp.model.dao.AuthorityDao; 
import csp.model.dao.CustomerDao; 
import csp.model.dao.ParkingLotDao; 



public class ChangeRoleAjaxController implements Controller { 
    CustomerDao  customerDao; 
    AuthorityDao authorityDao; 
    public CustomerDao getCustomerDao() { 
     return customerDao; 
    } 
    public void setCustomerDao(CustomerDao customerDao) { 
     this.customerDao = customerDao; 
    } 
    public AuthorityDao getAuthorityDao() { 
     return authorityDao; 
    } 
    public void setAuthorityDao(AuthorityDao authorityDao) { 
     this.authorityDao = authorityDao; 
    } 
    public ModelAndView handleRequest(HttpServletRequest request, 
      HttpServletResponse response) throws Exception {  

     System.out.println("Username: " + request.getParameter("user")); 
     Customer customer = customerDao.getUserByName(request.getParameter("user")); 
     Authority a = customer.getAuthority(); 
     if(a.getAuthority().equals("ROLE_USER")) 
      a.setAuthority("ROLE_OWNER"); 
     else if(a.getAuthority().equals("ROLE_OWNER")) 
      a.setAuthority("ROLE_USER"); 
     authorityDao.add(a); 
     customer.setAuthority(a); 
     customerDao.add(customer); 
     return new ModelAndView("/admin/ajax_changerole").addObject("role", a.getAuthority()); 
    } 

} 

接着控制器: 2. ChangeRoleFormController

 
package csp.spring.controller; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.springframework.security.providers.encoding.PasswordEncoder; 
import org.springframework.util.StringUtils; 
import org.springframework.validation.BindException; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.SimpleFormController; 

import csp.spring.security.SecurityUtils; 
import csp.model.Authority; 
import csp.model.Customer; 
import csp.model.dao.AuthorityDao; 
import csp.model.dao.CustomerDao; 

public class ChangeRoleFormController extends SimpleFormController { 

    CustomerDao  customerDao; 
    public CustomerDao getCustomerDao() { 
     return customerDao; 
    } 

    public void setCustomerDao(CustomerDao customerDao) { 
     this.customerDao = customerDao; 
    } 
    /* 
    protected Object formBackingObject(HttpServletRequest request) 
    throws Exception 
    { 
     return new Customer(); 
    }*/ 
    protected ModelAndView onSubmit(HttpServletRequest request, 
     HttpServletResponse response, Object command, BindException errors) throws Exception 
     { 
      Customer customer = customerDao.getUserByName(request.getParameter("user")); 
      String name = customer.getFirstName() + " " + customer.getLastName(); 
      String username = customer.getUserName(); 
      String role = customer.getAuthority().getAuthority(); 
      return new ModelAndView("/changerole").addObject("name", name) 
      .addObject("username", username).addObject("role", role).addObject("flag", true); 
     } 

} 

个我的两个JSP文件: 1. changerole.jsp changerole.jspchangerole.jsp

下一个JSP文件: 2. ajax_changerole.jsp

ajax_changerole.jsp

+0

你的问题太长;你的控制器代码和jsp屏幕截图对于有人理解你的问题是不必要的。 – craftsman 2011-05-25 21:32:49

+0

嗯,我完全同意我没有必要在这里发布我的控制器或jsp文件。我这样做的原因是因为我尝试了所有可能的方法来弄清楚我的代码可能出错,而我失败了。所以只要确保我不排除在不太可能的地方出现错误的可能性,并且有人可能会向我指出。 – SerotoninChase 2011-05-25 21:55:57

回答

1

广场这个地方,在你的JSP的顶部导入后JSTL标签库(假设你正在使用JSTL):

<c:set var="contextPath" value="${pageContext.request.contextPath}"/> 

,那么这个值给你的网址:

url : "${contextPath}/admin/ajaxchangerole.html?user=" + document.getElementById('userid').value 

作为一个附注,使用jQuery选择器而不是document.getElementById()是个好主意。它们易于使用,更具可读性和更多跨浏览器兼容性。

+0

嘿工匠,感谢您关于使用jQuery选择器而不是使用document.getElementById()的建议。我肯定会改变它的方式:)但回到我原来的问题,我尝试使用url:“$ {contextPath} /admin/ajaxchangerole.html?user=”,但它并没有进入控制器。只要我改变我的网址为:“$ {contextPath} /ajaxchangerole.html?user=”一切正常。我似乎不了解如何在URL中添加“/ admin”会导致如此多的问题。如果我直接访问url,它就可以工作,所以我知道spring并没有搞乱。 – SerotoninChase 2011-05-25 21:48:07