从extjs ajax调用写json文件

时间:2014-10-06 10:14:14

标签: java ajax json extjs

这是一个非常简单的问题。

目标:我想用extjs ajax调用更新.json文件。

1)Extjs代码

Ext.Ajax.request({
url : 'hello',                                                                                
headers: { 'Content-Type': 'application/json' },                                                                                                          
jsonData: {
  "username" : "admin",
  "emailId" : "admin@sivalabs.com"
    },
success: function (response) {
     //var jsonResp = Ext.util.JSON.decode(response.responseText);
     //Ext.Msg.alert("Info","UserName from Server : "+jsonResp.username);
     Ext.Msg.alert('success');
   },
failure: function (response) {
  //var jsonResp = Ext.util.JSON.decode(response.responseText);
  //Ext.Msg.alert("Error",jsonResp.error);
  Ext.Msg.alert('fail');
   }
});

2)控制器类 HelloController.java [路径: src \ com \ tutorialspoint ]

package com.tutorialspoint;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/hello")
public class HelloController{

   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");

      return "hello";
   }

}

3)Servlet文件 HelloWeb-servlet.xml [路径: src \ main \ webapp \ WEB-INF ]

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   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-3.0.xsd">

   <context:component-scan base-package="com.tutorialspoint" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>

</beans>

4)web.xml [路径: src \ main \ webapp \ WEB-INF ]

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>Sample Parent App</display-name>

    <servlet>
        <servlet-name>ParentServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>ParentServlet</servlet-name>
        <url-pattern>*.jsonp</url-pattern>
    </servlet-mapping>

    <servlet>
      <servlet-name>HelloWeb</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>2</load-on-startup>
   </servlet>

    <servlet-mapping>
        <servlet-name>HelloWeb</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>



    <filter>
        <filter-name>cross-origin</filter-name>
        <filter-class>com.mdstech.controller.SimpleCORSFilter</filter-class>
        <init-param>
            <param-name>allowedOrigins</param-name>
            <param-value>*</param-value>
        </init-param>
        <init-param>
            <param-name>allowedMethods</param-name>
            <param-value>GET,POST,OPTIONS,DELETE,PUT,HEAD</param-value>
        </init-param>
        <init-param>
            <param-name>allowedHeaders</param-name>
            <param-value>origin, content-type, accept, authorization</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>cross-origin</filter-name>
        <url-pattern>*</url-pattern>
    </filter-mapping>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">     
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
        </list>
    </property>     
 </bean>



</web-app>

当我调用ajax调用时,它会给我一个404错误

  

无法加载资源:服务器响应状态为404   (未找到)/parent/HelloWeb/hello.jsp

我对extjs很新,所以如果可以,请帮助我。

亲切的问候,

0 个答案:

没有答案