如何在MVC

时间:2015-05-27 08:44:47

标签: java javascript json jsp

我试图从数据库中检索数据并将其作为JSON从控制器返回,以便我可以在jsp中显示该数据的高图。 最初我试图在alert()中警告JSON数据以检查数据是否来临。我从控制器返回的 json未在警报中填充。有人可以帮我吗?

ReportModel.java



 import org.springframework.stereotype.Component;

@Component
public class ReportModel {
	int year;
	int population;
	
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public int getPopulation() {
		return population;
	}
	public void setPopulation(int population) {
		this.population = population;
	}

	@Override
	public String toString() {
		return "reportModel [year=" + year + ", population=" + population + "]";
	}
     	
   }




ReportDao



@Repository
public class ReportDao {
	


	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@SuppressWarnings("deprecation")
	@Autowired
	private SimpleJdbcTemplate simpleJdbcTemplate;
	

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}




	public void setSimpleJdbcTemplate(SimpleJdbcTemplate simpleJdbcTemplate) {
		this.simpleJdbcTemplate = simpleJdbcTemplate;
	}
	
	//method to retrieve the data from population table
	@SuppressWarnings("unchecked")
	public List<ReportModel> getReport() {
		final String reportQuery = "SELECT year, population from population";
		 List<ReportModel> report = new ArrayList<ReportModel>();

		 report = jdbcTemplate.query(
				reportQuery, new RowMapper<ReportModel>() {

					public ReportModel mapRow(ResultSet rs, int rowNum)
							throws SQLException {

						ReportModel model = new ReportModel();
						model.setYear(Integer.parseInt(rs.getString("year")));
						model.setPopulation(Integer.parseInt(rs.getString("population")));
						

						return model;
					}

				});
		
		return  report ;
	}
}
&#13;
&#13;
&#13;

ReportImpl

&#13;
&#13;
@Service
public class ReportImpl {
	
	@Autowired
	ReportDao reportDao; 
	private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ReportDao.class);
	 
     	public void setReportDao(ReportDao reportDao) {
		this.reportDao = reportDao;
	}
	
	public List<ReportModel> getReportModels() {
		return reportDao.getReport();
	}  

}
&#13;
&#13;
&#13;

ReportController

&#13;
&#13;
@Controller
@RequestMapping("/reportController")
public class ReportController {
	
	@Autowired
	ReportImpl reportImpl;
	
	public void setReportImpl(ReportImpl reportImpl) {
		this.reportImpl = reportImpl;
	}

	public ReportController() {
		super();
		System.out.println("inside Reportconstructor"); 
		}

	@RequestMapping(value = "/getreport", method = RequestMethod.GET)
	@ResponseBody
	 public Object getReportData()
    {
		System.out.println("inside report controller : get Report");
		List<ReportModel> reportModel =  reportImpl.getReportModels();
		Object json = null;
		ObjectMapper objectMapper = new ObjectMapper();
		  try {
			  
			  //display to console
			  json = objectMapper.readValue(
					     objectMapper.writeValueAsString(reportModel), Object.class);
		  
		  }catch(JsonGenerationException e)
		  {
			  e.printStackTrace();
		  } catch (JsonMappingException e)
		  {
			   e.printStackTrace();
		  } catch (IOException e) 
		  {
			   e.printStackTrace();
		  } 
		  
		  return json;
		 
	
    }
}
&#13;
&#13;
&#13;

report.jsp

&#13;
&#13;
<html>
  <head>
    <script type="text/javascript">

$(document).ready(function(){
    $("#button").click(function(){
        alert("The button is clicked.");
        
        
        <!--ajax call-->
        $.ajax({
    		type : "GET",
    		url : '/dispatcherServlet/ReportController/getReportData',
    		dataType : "json",
    	    contentType: "application/json",
    	    crossDomain:true,
    		success : function(data) {
    			//console.log(data);
    			//alert(success);
    			for(var i=0;i<data.length;i++)
    				{
    					alert(data[i].name);
    				}
    		}
    			
    			error : function(data) {
    				alert(data);
   				 }
        });
        <!--ajax call-->
        
        
    });
});

</script>
    </head>
  </html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

INCORRECT url:&#39; / dispatcherServlet / ReportController / getReportData&#39;,

CORRECT url:&#39; / dispatcherServlet / ReportController / getreport&#39;

问题在于您的请求映射。