首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

spring3.1 + mybatis 回来null正常,返回结果前台接收不到

2013-02-03 
spring3.1 + mybatis 返回null正常,返回结果前台接收不到这是我的web.xml?xml version1.0 encodingU

spring3.1 + mybatis 返回null正常,返回结果前台接收不到
这是我的web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 配置spring核心servlet -->
<servlet>
<servlet-name>frame</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>frame</servlet-name>
<!-- url配置为*.json,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为*.do,则不影响静态文件的访问 -->
<url-pattern>*.json</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>


这是我的frame-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:util="http://www.springframework.org/schema/util"  
    xmlns:tx="http://www.springframework.org/schema/tx"
    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-3.0.xsd  
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能--> 
<context:component-scan base-package="com" />

<!-- 使用annotation 自动注册事务--> 
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="*" />

<!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->    
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />



<!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射  -->  
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  

  
<!-- ③:对模型视图名称的解析,即在模型视图名称添加前后缀  -->  
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="" p:suffix=".jsp" p:viewClass = "org.springframework.web.servlet.view.JstlView" />  -->

</beans>



applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
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-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 配置数据源 -->
<bean id="dataSource_dbcp" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/frame?characterEncoding=GBK" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="maxActive" value="10"></property>
<property name="maxIdle" value="0" />
<property name="maxWait" value="200" />
</bean>

<bean id="defaultDataSource"
class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<constructor-arg>
<ref bean="dataSource_dbcp" />
</constructor-arg>
</bean>

<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="defaultDataSource" /<property name="mapperLocations" value="classpath:com/mapper/*/*.mapper.xml"/>
</bean>
<!-- 启用dao的注解方式 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="annotationClass"   value="org.springframework.stereotype.Repository" />
<property name="basePackage"       value="com.*" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!--声明事务管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="defaultDataSource"></property>
</bean>
</beans>



Controller中的方法

/**
 * 查询用户信息
 * @param request
 * @param response
 * @return
 */
@RequestMapping(baseUrl + "userList" + suffix)
@ResponseBody
public List<User> userList(HttpServletRequest request, HttpServletResponse response) {
    List<User> list = new ArrayList<User>();
    
    try
    {
// 获取用户信息
list = sysUserServiceImpl.qryList(request);

    catch (Exception e)
    {
log.info("用户信息查询失败。原因:" + e.getMessage());
e.printStackTrace();
}
    
return list;
}


ajax请求

function selUser(){
var uname = $("#uname").val();
var cdate = $("#cdate").datetimebox('getValue');
var u_status = $("#u_status").combobox('getValue');

$.ajax({
type: "POST",
url : "userList.json",
data:{uname:uname,cdate:cdate,u_status:u_status},
//dataType:"json",
cache:false,
success:function(data){
alert("ddddddddddd");
}
});
}


现在我遇到的问题是,如果controller中查到数据了,前台则会报异常,不会进入success,如果后台我直接返回null,则会进入success,这是怎么回事呢? spring null
[解决办法]
ajax你的有个返回类型吧。

[解决办法]
有可能是中间逻辑出现错误

热点排行