jQuery Spring MVC

  • Z6_208430
    了解作者
  • 199.1KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-21 02:35
    上传日期
例子程序是如何用spring mvc框架整合jQuery,jqGrid的例子
spring-jqgrid-integration (1).zip
内容介绍
/** * */ package org.krams.tutorial.controller; import java.util.List; import javax.annotation.Resource; import org.apache.log4j.Logger; import org.krams.tutorial.domain.User; import org.krams.tutorial.json.CustomGenericResponse; import org.krams.tutorial.json.CustomUserResponse; import org.krams.tutorial.service.IUserService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; /** * Handles CRUD requests for users * */ @Controller @RequestMapping("/crud") public class UserController { protected static Logger logger = Logger.getLogger("controller"); @Resource(name="userService") private IUserService userService; /** * The default method when a request to /users is made. * This essentially retrieves all users, which are wrapped inside a CustomUserResponse object. * The object is automatically converted to JSON when returning back the response. * The @ResponseBody is responsible for this behavior. */ @RequestMapping(method = RequestMethod.GET) public @ResponseBody CustomUserResponse getAll( ) { logger.debug("Received request to get all users"); // Retrieve all users from the service List<User> users = userService.getAll(); // Initialize our custom user response wrapper CustomUserResponse response = new CustomUserResponse(); // Assign the result from the service to this response response.setRows(users); // Assign the total number of records found. This is used for paging response.setRecords( String.valueOf(users.size()) ); // Since our service is just a simple service for teaching purposes // We didn't really do any paging. But normally your DAOs or your persistence layer should support this // Assign a dummy page response.setPage( "1" ); // Same. Assign a dummy total pages response.setTotal( "10" ); // Return the response // Spring will automatically convert our CustomUserResponse as JSON object. // This is triggered by the @ResponseBody annotation. // It knows this because the JqGrid has set the headers to accept JSON format when it made a request // Spring by default uses Jackson to convert the object to JSON return response; } /** * Edit the current user. */ @RequestMapping(value = "/edit", method = RequestMethod.POST) public @ResponseBody CustomGenericResponse edit( @RequestParam("id") String id, @RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName ) { logger.debug("Received request to edit user"); // Construct our user object // Assign the values from the parameters User user = new User(); user.setId( Long.valueOf(id) ); user.setFirstName(firstName); user.setLastName(lastName); // Do custom validation here or in your service // Call service to edit Boolean success = userService.edit(user); // Check if successful if ( success == true ) { // Success. Return a custom response CustomGenericResponse response = new CustomGenericResponse(); response.setSuccess(true); response.setMessage("Action successful!"); return response; } else { // A failure. Return a custom response as well CustomGenericResponse response = new CustomGenericResponse(); response.setSuccess(false); response.setMessage("Action failure!"); return response; } } /** * Add a new user */ @RequestMapping(value = "/add", method = RequestMethod.POST) public @ResponseBody CustomGenericResponse add( @RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName ) { logger.debug("Received request to add a new user"); // Construct our new user object. Take note the id is not required. // Assign the values from the parameters User user = new User(); user.setFirstName(firstName); user.setLastName(lastName); // Do custom validation here or in your service // Call service to add Boolean success = userService.add(user); // Check if successful if ( success == true ) { // Success. Return a custom response CustomGenericResponse response = new CustomGenericResponse(); response.setSuccess(true); response.setMessage("Action successful!"); return response; } else { // A failure. Return a custom response as well CustomGenericResponse response = new CustomGenericResponse(); response.setSuccess(false); response.setMessage("Action failure!"); return response; } } /** * Delete an existing user */ @RequestMapping(value = "/delete", method = RequestMethod.POST) public @ResponseBody CustomGenericResponse delete( @RequestParam("id") String id ) { logger.debug("Received request to delete an existing user"); // Construct our user object. We just need the id for deletion. // Assign the values from the parameters User user = new User(); user.setId( Long.valueOf(id) ); // Do custom validation here or in your service // Call service to add Boolean success = userService.delete(user); // Check if successful if ( success == true ) { // Success. Return a custom response CustomGenericResponse response = new CustomGenericResponse(); response.setSuccess(true); response.setMessage("Action successful!"); return response; } else { // A failure. Return a custom response as well CustomGenericResponse response = new CustomGenericResponse(); response.setSuccess(false); response.setMessage("Action failure!"); return response; } } }
评论
    相关推荐
    • Spring MVC
      Spring MVC使用注解配置。。。。。。。。。。。。。。。。。。。。。
    • spring mvc
      本代码是spring mvc的一个小demo 只是完成mvc的一个实例。没有复杂的业务逻辑 本demo只是适合刚学习Spring mvc的同行
    • spring mvc
      spring mvc spring mvcspring mvc
    • Spring MVC
      一个简单的spring mvc登录案例,采用的是annotation方式。
    • spring mvc
      spring mvc 开发所需要的全部工具、jar包
    • spring mvc
      spring mvc spring 配置文件
    • Spring MVC
      一个spring mvc 小demo.
    • spring mvc
      spring mvc最小代码。spring mvc最小代码。spring mvc最小代码。spring mvc最小代码。
    • Spring MVC
      spring mvc开发所需要的所有jar包,以及基本的spring集合
    • spring mvc
      一个spring mvc 的小实例,较好的理解spring mvc.