springMVC+spring+MyBatis框架,ajax请求总是访问不到路径
spring-servlet.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="messageConverters">
<list>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射,添加拦截器,类级别的处理器映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>
<context:annotation-config />
<context:component-scan base-package="com.sys.service..*" />
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"></bean>
<!-- 设置使用注解的类所在的jar包 -->
<context:component-scan base-package="com.sys.controller"></context:component-scan>
<!-- 注解扫描包 -->
<!-- <context:component-scan base-package="com.sys.controller" /> -->
<!-- 开启注解 -->
<mvc:annotation-driven />
<!-- 静态资源访问 -->
<!-- <mvc:resources location="/img/" mapping="/img/**" />
<mvc:resources location="/js/" mapping="/js/**" /> -->
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
<mvc:resources mapping="/login.html" location="/" />
<mvc:resources mapping="/index.html" location="/" />
<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/html/"></property>
<property name="suffix" value=".html"></property>
</bean>
<!-- ①:注解扫描,对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 , -->
<context:component-scan base-package="com.sys">
<context:include-filter type="regex" expression="com.sys.controller.*Controller" />
<context:exclude-filter type="regex" expression="com.sys.service.impl.*Impl" />
</context:component-scan>
<!-- 文件解析上传文件时用到 -->
<!-- <bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="10485760000" />
<property name="maxInMemorySize" value="40960" />
</bean> -->
</beans>
html代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户注册</title>
<script type="text/javascript" src="../js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="../js/jquery.query-2.1.7.js"></script>
<script type="text/javascript" src="../js/common.js" charset="utf-8"></script>
<script type="text/javascript" src="../js/jquery-1.9.1.min.js" charset="utf-8"></script>
<script type="text/javascript">
function register(){
var uname = $("#uname").val();
var upwd = $("#upwd").val();
alert(1);
$.ajax({
url : "/ssmTest/users/addUser",
data : {"user.uname":uname,"user.upwd":upwd},
type : "POST",
dataType : "json",
success : function(result){
if(result.res == true){
alert("注册成功!");
}else{
alert("注册失败!");
}
}
});
}
function clearText(){
$("#uname").val("");
$("#upwd").val("");
}
</script>
</head>
<body>
用户名:
<input type="text" id="uname" size="20" maxlength="20"><br>
密 码:
<input type="text" id="upwd" size="20" maxlength="16"><br>
<input type="submit" value="提交" onclick="register()">
<input type="submit" value="重置" onclick="clearText()">
</body>
</html>
Controller:
package java.com.sys.controller;
import java.com.sys.entity.Users;
import java.com.sys.service.IUserService;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value="/users")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping(value="/addUser")
public Map<String, Object> addUser(Users user){
return userService.addUser(user);
}
}