2015-07-10 40 views
0

enter image description here我用spring mvc和ajax.i传递列表从控制器,我想警告它从ajax调用,但警报不从列表中获取数据,但HTML代码。当调用阿贾克斯调用不警报数据

控制器:

@RequestMapping("/AddUpdateCustomer") 
    public String redirectCustomer(Map<String, Object> map) { 
     map.put("customerList", customerService.listCustomer()); 

     return "AddUpdateCustomer"; 
    } 

Ajax调用:

<script type="text/javascript"> 

     function LoadDataToThegrid() { 

     // get the form values 

     //var name = $('#name').val(); 

     //var education = $('#education').val(); 



     $.ajax({ 

     type: "POST", 

     url: "AddUpdateCustomer.html", 

     //data: "name=" + name + "&education=" + education, 

     success: function(data){ 

     // we have the response 

     alert(data); 

     }, 

     error: function(e){ 

     alert('Error: ' + e); 

     } 

     }); 

     } 

     </script> 

被ALRT为:

全HTML代码给出的警报,但我想要得到的数据列表的警报。

网站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_2_5.xsd" 
    id="WebApp_ID" version="2.5"> 
    <display-name>Spring3-Hibernate</display-name> 
    <welcome-file-list> 
     <welcome-file>redirect.jsp</welcome-file> 
    </welcome-file-list> 
    <servlet> 
     <servlet-name>spring</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>spring</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

为spring-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:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:lang="http://www.springframework.org/schema/lang" 
    xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 
     http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <context:annotation-config /> 
    <context:component-scan base-package="net.opticare" /> 

    <!--<bean id="jspViewResolver"--> 
     <!--class="org.springframework.web.servlet.view.InternalResourceViewResolver">--> 
     <!--<property name="viewClass"--> 
      <!--value="org.springframework.web.servlet.view.JstlView" />--> 
     <!--<property name="prefix" value="/WEB-INF/jsp/" />--> 
     <!--<property name="suffix" value=".jsp" />--> 
    <!--</bean> --> 
<!-- newly added by me --> 

    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
     <property name="viewClass"> 
      <value> 
       org.springframework.web.servlet.view.tiles2.TilesView 
      </value> 
     </property> 
    </bean> 
    <bean id="tilesConfigurer" 
     class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"> 
     <property name="definitions"> 
      <list> 
       <value>/WEB-INF/tiles.xml</value> 
      </list> 
     </property> 
    </bean> 

<!-- --> 
    <bean id="messageSource" 
     class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
     <property name="basename" value="classpath:messages" /> 
     <property name="defaultEncoding" value="UTF-8" /> 
    </bean> 
    <bean id="propertyConfigurer" 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
     p:location="/WEB-INF/jdbc.properties" /> 

    <bean id="dataSource" 
     class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" 
     p:driverClassName="${jdbc.driverClassName}" 
     p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" 
     p:password="${jdbc.password}" /> 


    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="configLocation"> 
      <value>classpath:hibernate.cfg.xml</value> 
     </property> 
<!--  <property name="configurationClass"> --> 
<!--   <value>org.hibernate.cfg.AnnotationConfiguration</value> --> 
<!--  </property> --> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${jdbc.dialect}</prop> 
       <prop key="hibernate.show_sql">true</prop> 
      </props> 
     </property> 
    </bean> 
    <mvc:resources mapping="/resources/**" location="/resources/" /> 
    <mvc:annotation-driven /> 
    <tx:annotation-driven /> 

    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 




</beans> 

回答

0

添加注释@ResponseBody

@RequestMapping("/AddUpdateCustomer") 
@ResponseBody 
public String redirectCustomer(Map<String, Object> map) { 
    map.put("customerList", customerService.listCustomer()); 
    return "AddUpdateCustomer"; 
} 
+0

重定向整个页面是空的时候和印有“AddUpdateCustomer” – edmaa

0

我不认为呼叫到达您的控制器。 ajax调用实际上是获取AddUpdateCustomer.html的html。并发布您的web.xml和dispatcher-servlet.xml配置以获得进一步的说明。

0

你可以让你的控制器注释与@RestController 或者你可以注释与@ResponseBody的处理方法如下图所示

@RequestMapping("/AddUpdateCustomer") 
@ResponseBody 
public List<Customer> redirectCustomer() { 
     return customerService.listCustomer(); 
} 
+0

出错“的标识的资源此请求只能根据请求“接受”头文件生成具有不可接受的特征的响应。“ – edmaa

+0

>尝试将ajax调用从'POST' >更改为'GET'或添加@RequestMapping(“ .XML – Jebil