2015-07-10 34 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