首页>代码>ssm框架+easyui开发简单demo实例>/web/src/com/ajxm/web/controller/UserController.java
package com.ajxm.web.controller;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ajxm.web.entity.User;
import com.ajxm.web.service.IUserService;

@Controller
@RequestMapping(value = "/user")
public class UserController {
	@Resource(name = "userService")
	IUserService userService;

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(String uname,String password,RedirectAttributes rediect) {
		try {
			User result = userService.login(uname, password);
			rediect.addFlashAttribute("user", result);
			return "redirect:/main";
		} catch (Exception e) {
			return "errors/500";
		}
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST)
	@ResponseBody
	public String register(User user) {
		try {
			userService.insertUser(user);
			return "{\"code\":\"success\"}";
		} catch (Exception e) {
			return "{\"code\":\"error\"}";
		}
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete(String userid) {
		try {
			userService.deleteUser(userid);
			return "{\"code\":\"success\"}";
		} catch (Exception e) {
			return "{\"code\":\"error\"}";
		}
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public String update(User user) {
		try {
			userService.updateUser(user);
			return "{\"code\":\"success\"}";
		} catch (Exception e) {
			return "{\"code\":\"error\"}";
		}
	}

	@RequestMapping(value = "/idquery", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> query(String userid) {
		try {
			Map<String,Object> map = new HashMap<String, Object>();
			User user = userService.queryUserById(userid);
			List<User> list = new LinkedList<User>();
			list.add(user);
			map.put("total","1");
			map.put("rows", list);
			return map;
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/nquery", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> queryByName(String nickname) {
		try {
			Map<String,Object> map = new HashMap<String, Object>();
			List<User> list = userService.queryUserByName(nickname);
			map.put("total", list.size());
			map.put("rows", list);
			return map;
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public List<User> getList() {
		try {
			return userService.getList();
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/page", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getPageList(int index, int size) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<User> list = userService.getPageList(index, size);
			map.put("total", "14");//这里设置固定为14,是因为测试的时候只有14条数据,这里可以是为了方便
			map.put("rows", list);
		} catch (Exception e) {
			map.put("total", 0);
			map.put("rows", null);
		}
		return map;
	}
}
最近下载更多
yeyuuu  LV6 2023年5月20日
h_z_feng  LV1 2022年12月24日
potato1234  LV6 2022年3月22日
amour1  LV11 2021年12月5日
543666826  LV33 2021年11月19日
w384553037  LV1 2021年11月4日
wubinbin  LV11 2021年10月10日
heqian  LV16 2021年9月28日
ljl123456321  LV1 2021年9月11日
木木不爱安妮酱  LV10 2021年7月3日
最近浏览更多
h G24741  LV2 3月12日
Henew168  LV2 2023年11月19日
mmmhhh 2023年11月13日
暂无贡献等级
张朕朕  LV3 2023年9月1日
yeyuuu  LV6 2023年5月20日
milou123  LV2 2023年3月7日
yiposhop  LV4 2023年2月16日
LITIANYU084414  LV11 2023年1月1日
h_z_feng  LV1 2022年12月24日
kaonixiwa  LV3 2022年11月15日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友