当调用ajax调用不是警报数据时

时间:2015-07-10 05:41:14

标签: ajax spring-mvc apache-tiles

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>

得到如下:

完整的html代码提供警报,但我希望将数据列表作为警报。

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_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>

弹簧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>

3 个答案:

答案 0 :(得分:0)

添加注释@ResponseBody

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

答案 1 :(得分:0)

我不认为这个电话正在接通你的控制器。 ajax调用实际上是获取AddUpdateCustomer.html的html。并发布您的web.xml和dispatcher-servlet.xml配置以进一步说明。

答案 2 :(得分:0)

您可以使用data = Hotel::where('name', 'LIKE', "%$keyword%") ->with( array( 'city' => function($query){ $query->select( array('cities.id', 'cities.description')); }) ) ->get(array('hotels.id', 'city_id', 'hotels.name', 'hotels.description as desc')); 对控制器进行注释 或者您可以使用@RestController注释处理程序方法,如下所示

@ResponseBody