Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';在AJAX的Spring注释中找不到控制器_Java_Jquery_Ajax_Spring_Spring Annotations - Fatal编程技术网

Java Can';在AJAX的Spring注释中找不到控制器

Java Can';在AJAX的Spring注释中找不到控制器,java,jquery,ajax,spring,spring-annotations,Java,Jquery,Ajax,Spring,Spring Annotations,我试图在Spring3.0中通过AJAX加载数据,但是AJAX URL在Spring中找不到控制器,我不知道如何解决这个问题 我知道,当服务器启动时,它会查找URL并获取数据,但在这里,我无法在spring中正确地装入注释,我在web上搜索了很多次,但都没有成功 我的Java类: package springactiontest; import java.util.List; import javax.servlet.http.HttpSession; import org.json.si

我试图在Spring3.0中通过AJAX加载数据,但是AJAX URL在Spring中找不到控制器,我不知道如何解决这个问题

我知道,当服务器启动时,它会查找URL并获取数据,但在这里,我无法在spring中正确地装入注释,我在web上搜索了很多次,但都没有成功

我的Java类:

package springactiontest;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import Dao.dao;


@Controller
//@RequestMapping("/employee")
public class mainclass {

    @RequestMapping(value="/AddUser",method=RequestMethod.GET)

public @ResponseBody static String data(ModelMap model, HttpSession session) {

        System.err.println("err ocured");

          ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

          setvalueclass studentJDBCTemplate =(setvalueclass)context.getBean("actionclass");

          System.out.println("------list district--------" );

        JSONArray newtest=new JSONArray();

          List<dao> students = studentJDBCTemplate.listStudents();
          for (dao record : students) 
          {

             JSONObject ob=new JSONObject();
             System.out.print("ID test: " + record.getDistrict());
             ob.put("distrct", record.getDistrict());
             newtest.add(ob);
          }

          System.err.println("error");


          String res=newtest.toString();

          System.err.println("error"+res);

        return res;

     }

}
Jsp:
$("document").ready(function () {

        alert("distrcict");

        dist_pop();

});

function dist_pop() {

var urlService="http://tamilnilam:8080/SpringTest";
        $.ajax({
        url: urlService +'/AddUser',
        type: 'GET',
        dataType: 'jsonp',

        contentType: "application/json",
          success: function (data) {

            alert("sucess")          

        },
        error: function (jqXHR, exception) {

            alert("Error Occured in dist pop");

        }

    });

}
封装测试;
导入java.util.List;
导入javax.servlet.http.HttpSession;
导入org.json.simple.JSONArray;
导入org.json.simple.JSONObject;
导入org.springframework.context.ApplicationContext;
导入org.springframework.context.support.ClassPathXmlApplicationContext;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.ModelMap;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.ResponseBody;
导入Dao.Dao;
@控制器
//@请求映射(“/employee”)
公共类主类{
@RequestMapping(value=“/AddUser”,method=RequestMethod.GET)
public@ResponseBody静态字符串数据(ModelMap模型,HttpSession会话){
System.err.println(“err ocured”);
ApplicationContext context=new ClassPathXmlApplicationContext(“ApplicationContext.xml”);
setvalueclass studentJDBCTemplate=(setvalueclass)context.getBean(“actionclass”);
System.out.println(“----列表区----”;
JSONArray newtest=新的JSONArray();
List students=studentJDBCTemplate.listStudents();
对于(dao记录:学生)
{
JSONObject ob=新的JSONObject();
System.out.print(“ID测试:+record.getDistrict());
ob.put(“District”,record.getDistrict());
新增(ob);
}
System.err.println(“错误”);
字符串res=newtest.toString();
System.err.println(“error”+res);
返回res;
}
}
Jsp:
$(“文档”).ready(函数(){
警报(“District”);
dist_pop();
});
函数dist_pop(){
var urlService=”http://tamilnilam:8080/SpringTest";
$.ajax({
url:urlService+'/AddUser',
键入:“GET”,
数据类型:“jsonp”,
contentType:“应用程序/json”,
成功:功能(数据){
警惕(“成功”)
},
错误:函数(jqXHR,异常){
警报(“dist pop中发生错误”);
}
});
}
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:context="http://www.springframework.org/schema/context"
    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="springactiontest.setvalueclass"/>


   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="org.postgresql.Driver"/>
      <property name="url" value="jdbc:postgresql://10.163.2.165:5434/land_rural"/>
      <property name="username" value="postgres"/>
      <property name="password" value="postgres"/>
   </bean> 


   <bean id="actionclass" class="springactiontest.setvalueclass">
      <property name="dataSource"  ref="dataSource" />    
   </bean>
</beans>

似乎缺少
组件扫描
,它可以帮助Spring检测并实例化用
@component
@Service
@Repository
@Controller
@Endpoint
等注释的Springbean

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    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.your.controller.package" />

    // Rest of the bean defiantions
</beans>

//其余的豆子挑战

仅供参考:

您是否获得404 http错误?请删除该注释代码,并保持代码一致。就像requestmapping和方法定义之间没有空格一样。第二,你的错误是什么?如何无误地说出错误。第三,请同时粘贴您的servlet-context.xml或类似内容。@fran Montero yes 404error@user3607180你能分享你的bean定义文件吗?@WeareBorg当我点击浏览器控制台时,我得到404错误,我需要在哪里添加itbean.xml??更新后的ITA有什么用?正如你所说的404错误只是暂时还没有出现solved@user3607180您可以共享更新的bean定义文件吗?