问题描述
|
嘿,我对ajax uri的工作方式完全感到困惑。请看下面的代码:
<script>
function clickHandler(){
$.ajax({
**url : \"http://localhost:8080/csp/ajaxchangerole.html?user=\" + document.getElementById(\'userid\').value,**
dataType: \'text\',success : function (data) {
$(\"#roleval\").html(data);
}
});
}
即使我将网址更改为这种方式,它也有效
<script>
function clickHandler(){
$.ajax({
**url : \"ajaxchangerole.html?user=\" + document.getElementById(\'userid\').value,success : function (data) {
$(\"#roleval\").html(data);
}
});
}
但是,如果我将网址更改为下面提到的两个网址中的任何一个,它甚至都不会调用控制器。我不确定为什么。有人可以解释我需要在以下指定的URL处调用控制器吗?
网址:\“ admin / ajaxchangerole.html?user = \” + document.getElementById(\'userid \')。value,
要么
网址:\“ http:// localhost:8080 / csp / admin / ajaxchangerole.html?user = \” + document.getElementById(\'userid \')。value,
这是我的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部分正常工作。任何帮助是极大的赞赏。提前致谢。
问候,
5-羟色胺
菲
我的控制器:
1.ChangeRoleAjaxController
包csp.spring.controller;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入org.apache.commons.logging.Log;
导入org.apache.commons.logging.LogFactory;
导入org.springframework.web.servlet.ModelAndView;
导入org.springframework.web.servlet.mvc.Controller;
导入csp.model.Authority;
导入csp.model.Customer;
导入csp.model.dao.AuthorityDao;
导入csp.model.dao.CustomerDao;
导入csp.model.dao.ParkingLotDao;
公共类ChangeRoleAjaxController实现Controller {
CustomerDao customerDao;
AuthorityDao AuthorityDao;
public CustomerDao getCustomerDao(){
退还客户
}
public void setCustomerDao(CustomerDao customerDao){
this.customerDao = customerDao;
}
public AuthorityDao getAuthorityDao(){
归还授权
}
public void setAuthorityDao(AuthorityDao AuthorityDao){
this.authorityDao = AuthorityDao;
}
公共ModelAndView handleRequest(HttpServletRequest请求,
HttpServletResponse响应)引发Exception {
System.out.println(\“用户名:\” + request.getParameter(\“ user \”)));
客户客户= customerDao.getUserByName(request.getParameter(\“ user \”));
权限a = customer.getAuthority();
if(a.getAuthority()。equals(\“ ROLE_USER \”))
a.setAuthority(\“ ROLE_OWNER \”);
否则if(a.getAuthority()。equals(\“ ROLE_OWNER \”))
a.setAuthority(\“ ROLE_USER \”);
AuthorityDao.add(a);
customer.setAuthority(a);
customerDao.add(customer);
返回新的ModelAndView(\“ / admin / ajax_changerole \”)。addobject(\“ role \”,a.getAuthority());
}
}
下一个控制器:
2. ChangeRoleFormController
包csp.spring.controller;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入org.springframework.security.providers.encoding.PasswordEncoder;
导入org.springframework.util.StringUtils;
导入org.springframework.validation.BindException;
导入org.springframework.web.servlet.ModelAndView;
导入org.springframework.web.servlet.mvc.SimpleFormController;
导入csp.spring.security.SecurityUtils;
导入csp.model.Authority;
导入csp.model.Customer;
导入csp.model.dao.AuthorityDao;
导入csp.model.dao.CustomerDao;
公共类ChangeRoleFormController扩展了SimpleFormController {
CustomerDao customerDao;
public CustomerDao getCustomerDao(){
退还客户
}
public void setCustomerDao(CustomerDao customerDao){
this.customerDao = customerDao;
}
/ *
受保护的对象formbackingObject(HttpServletRequest request)
抛出异常
{
返回新的Customer();
} * /
受保护的ModelAndView onSubmit(HttpServletRequest请求,
HttpServletResponse响应,对象命令,BindException错误)引发Exception
{
客户客户= customerDao.getUserByName(request.getParameter(\“ user \”));
字符串名称= customer.getFirstName()+ \“ \” + customer.getLastName();
字符串用户名= customer.getUserName();
字符串角色= customer.getAuthority()。getAuthority();
返回新的ModelAndView(\“ / changerole \”)。addobject(\“ name \”,name)
.addobject(\“用户名\”,用户名).addobject(\“角色\”,角色).addobject(\“ flag \”,true);
}
}
我的两个jsp文件:
1. changerole.jsp
下一个jsp文件:
2. ajax_changerole.jsp
解决方法
导入JSTL标签库后,将其放置在JSP顶部(假设您正在使用JSTL):
<c:set var=\"contextPath\" value=\"${pageContext.request.contextPath}\"/>
然后将此值提供给您的网址:
url : \"${contextPath}/admin/ajaxchangerole.html?user=\" + document.getElementById(\'userid\').value
附带说明一下,使用jQuery选择器而不是document.getElementById()
是一个好主意。它们易于使用,更具可读性并且更兼容跨浏览器。