小弟在controller层中,做了如下定义
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
@RequestMapping(value = "/testAjax.html")
@ResponseBody
public String testAjax(HttpServletRequest request,
HttpServletResponse response)
{
try
{
String param = URLDecoder
.decode(request.getParameter("param"), "utf-8").trim()
.toString();
return "ajax请求成功:" + param;
}
catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return "false";
}
}
}
然后使用http://localhost:8080/alan-springmvc-web/userInfo/testAjax.html,来访问该servlet,提示404Error.
代码没有问题,URL也没有错。后来仔细想想,应该是web.xml中配置有问题,因为responsebody里面的value对于的地址是以“.html”结尾的,所以该请求将被default servlet拦截,从而到相应的静态文件中区找名为testAjax.html的静态网页,但是这个网页其实并不存在,当然会报404Error咯。
如果不是在web.xml中配置了对相应静态文件请求拦截的朋友,也有可能是在Spring的配置文件中进行了如下配置,也可能导致servlet调用了默认的default servlet处理方式,使得配置在controller中的请求地址有被错误处理。关于mvc配置请看该链接:web.xml对静态资源的处理之spring配置
如:
- <mvc:default-servlet-handler/>
或者
- <!-- 对静态资源文件的访问 -->
- <mvc:resources mapping="/**/.html" location="/" />