ajax role.html,ajax jquery url specification

博客内容涉及一个关于Ajax调用控制器时遇到的问题。作者发现URL的路径不同导致控制器是否被调用的结果不同,主要涉及到`/admin`前缀的使用。在代码示例中,当移除`/admin`前缀时,Ajax请求才能成功执行。问题可能与URL映射配置有关,特别是`ChangeRoleAjaxController`和`ChangeRoleFormController`的设置。博客寻求对这一问题的解释和解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hey I'm totally confused with the way the ajax uri works. Please look at the code below:

function clickHandler(){

$.ajax({

**url : "http://localhost:8080/csp/ajaxchangerole.html?user=" + document.getElementById('userid').value,**

dataType: 'text',

success : function (data) {

$("#roleval").html(data);

}

});

}

It also works even if I change the url to this fashion

function clickHandler(){

$.ajax({

**url : "ajaxchangerole.html?user=" + document.getElementById('userid').value,**

dataType: 'text',

success : function (data) {

$("#roleval").html(data);

}

});

}

But if I change the url to either of the two urls mentioned below, it doesn't even call the controller. I'm not sure why. Can someone explain what I need to call the controller at the below specified urls?

url : "admin/ajaxchangerole.html?user=" + document.getElementById('userid').value,

OR

Here is my snippet from my xml file that has the mappings for the above urls:

class="csp.spring.controller.admin.ChangeRoleAjaxController">

I can't understand why I have to remove "/admin" from the above two mappings to get this ajax part working. Any help is greatly appreciated. Thanks in advance.

Regards,

Serotonin Chase

Fyi,

my controllers:

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());

}

}

Next controller:

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);

}

}

My two jsp files:

1. changerole.jsp

2Vl9w.jpg

ikbBw.jpg

Next jsp file:

2. ajax_changerole.jsp

aDi8L.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值