手机端访问controller_SpringMVC综合使用手机管理系统Controller层开发

1. beans.xml的配置

class="org.springframework.jdbc.datasource.DataSourceTransactionManager"p:dataSource-ref="dataSource" />

class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">

0

UTF-8

0.##########

yyyy-MM-dd HH:mm:ss

true

ignore

class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">

.html

application/json;charset=UTF-8

text/plain;charset=UTF-8

2. LoginController

package com.green.phonemanage.controller;

import java.util.ArrayList;

import java.util.List;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;

import com.green.phonemanage.model.MenuItem;

@Controllerpublic classLoginController {

@RequestMapping(value="/web/toIndex",method=RequestMethod.GET)public ModelAndView loginToIndex(String loginName,int role,intuserId){

ModelAndView andView=new ModelAndView("index");

andView.addObject("loginName",loginName);

andView.addObject("role", role);

andView.addObject("userId", userId);returnandView;

}

}

3. UserController

package com.green.phonemanage.controller;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

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.RequestParam;

import org.springframework.web.bind.annotation.RestController;

import com.green.phonemanage.dao.UserDao;

import com.green.phonemanage.model.Msg;

import com.green.phonemanage.model.SearchForm;

import com.green.phonemanage.model.User;

import com.green.phonemanage.model.UserMsg;

import com.green.phonemanage.service.IUserService;

@RestController

public class UserController {

@Autowired

private IUserService userService;

public void setUserService(IUserService userService) {

this.userService = userService;

}

@Autowired

private UserDao userDao;

public void setUserDao(UserDao userDao) {

this.userDao = userDao;

}

/*

* �û���¼

*

* @param:��¼��

*

* @param:����

*

* @param:��ɫ��1.�ͻ���2������Ա

*

* @return:��¼��Ϣ

*/

@RequestMapping(value = "/user/login", method = RequestMethod.POST)

public UserMsg login(@RequestParam String loginName, @RequestParam String passwd,

@RequestParam int role) {

User user = new User();

user.setLoginName(loginName);

user.setPasswd(passwd);

user.setRole(role);

return userService.login(user);

}

/*

* �û�ע��

*

* @param:�û���Ϣ

*

* @return:�û�ע����Ϣ

*/

@RequestMapping(value = "user/register", method = RequestMethod.POST,produces="application/json",consumes="application/json")

public UserMsg register(@RequestBody User user) {

user.setStatus(1);

return userService.register(user);

}

/*

* 查询登录名是否存在

*

* @param:登录名

*

* @return:状态

*/

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

public UserMsg nameExist(@RequestParam("loginName") String loginName) {

UserMsg um = new UserMsg();

um.setMsg("登录名不存在");

um.setStatus(1);

Integer id = userDao.findByLoginName(loginName);

if (id != null) {

um.setStatus(0);

um.setMsg("登录名已经存在");

}

return um;

}

/*

* ͨ��id��ȡ�û���Ϣ

*

* @param:�û�id

*

* @return:�û���Ϣ

*/

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

public User findUser(@RequestParam("id") int id) {

return userDao.findById(id);

}

/*

* �û���Ϣ��ҳ��ѯ

*

* @param:��ҳ��Ϣ

*

* @return:�û���Ϣ�б�

*/

@RequestMapping(value = "user/finds", method = RequestMethod.POST)

public Map finds(SearchForm searchForm) {

searchForm.setPageIndex(searchForm.getPageIndex() * searchForm.getPageSize());

List users = userDao.finds(searchForm);

Map datas = new HashMap();

datas.put("data", users);

datas.put("total", userDao.totals()==null?0:userDao.totals());

return datas;

}

/*

* ɾ��ָ�����û�

*

* @param:�û�id

*

* @return:��Ϣ

*/

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

public Msg remove(@RequestParam("id") int id) {

Msg rmMsg = new Msg();

rmMsg.setId(id);

rmMsg.setMsg("ɾ���ɹ�");

rmMsg.setState(1);

try {

userDao.rmove(id);

} catch (Exception e) {

rmMsg.setState(0);

rmMsg.setMsg("ɾ��ʧ��");

}

return rmMsg;

}

/*

* �޸��û���Ϣ

*

* @param:�û�id

*

* @return:�û�״̬

*/

@RequestMapping(value = "user/update", method = RequestMethod.POST)

public Msg update(User user) {

Msg rmMsg = new Msg();

rmMsg.setId(user.getId());

rmMsg.setMsg("�޸ijɹ�");

rmMsg.setState(1);

try {

userDao.update(user);

} catch (Exception e) {

rmMsg.setState(0);

rmMsg.setMsg("�޸�ʧ��");

}

return rmMsg;

}

/*

* �û�����

*

* @param:�û�id

*

* @return:���ز���״̬

*/

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

public Msg freeze(@RequestParam("id") int id) {

Msg msg = new Msg();

msg.setId(id);

msg.setState(1);

msg.setMsg("成功");

try {

userDao.freeze(id);

} catch (Exception e) {

msg.setState(0);

msg.setMsg("失败");

}

return msg;

}

/*

* �û��ⶳ

*

* @param:�û�id

*

* @return:���ز���״̬

*/

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

public Msg unfreeze(@RequestParam("id") int id) {

Msg msg = new Msg();

msg.setId(id);

msg.setState(1);

msg.setMsg("成功");

try {

userDao.unfreeze(id);

} catch (Exception e) {

msg.setState(0);

msg.setMsg("失败");

}

return msg;

}

}

4. ClientController

package com.green.phonemanage.controller;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.ModelAttribute;

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.RequestParam;

import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonAnyFormatVisitor;

import com.fasterxml.jackson.databind.util.JSONPObject;

import com.green.phonemanage.dao.CellPhoneDao;

import com.green.phonemanage.dao.ClientDao;

import com.green.phonemanage.model.Client;

import com.green.phonemanage.model.Msg;

import com.green.phonemanage.model.SearchForm;

import com.green.phonemanage.service.IClientService;

;

@RestController

public class ClientController {

private Logger logger = LoggerFactory.getLogger(getClass());

@Autowired

private IClientService clientService;

public void setClientService(IClientService clientService) {

this.clientService = clientService;

}

@Autowired

private CellPhoneDao cellPhoneDao;

public void setCellPhoneDao(CellPhoneDao cellPhoneDao) {

this.cellPhoneDao = cellPhoneDao;

}

@Autowired

private ClientDao clientDao;

public void setClientDao(ClientDao clientDao) {

this.clientDao = clientDao;

}

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

public void tst(@RequestParam("id") int id) {

System.out.println("This is a test.");

}

/*

* 通鏌ユ壘瀹㈡埛

*

* @param:瀹㈡埛id

*

* @return:瀹㈡埛淇℃伅

*/

@RequestMapping(value = "/client/find", method = RequestMethod.GET, produces = "application/json", consumes = "application/json")

public Client findUser(@RequestParam("id") int id) {

return clientDao.findById(id);

}

/*

* 鍒嗛〉鑾峰彇瀹㈡埛淇℃伅

*

* @param:鍒嗛〉淇℃伅

*

* @return:鍒嗛〉鎵�鏈夊鎴蜂俊鎭�

*/

@RequestMapping(value = "client/finds", method = RequestMethod.POST)

public Map finds(SearchForm searchForm) {

searchForm.setPageIndex(searchForm.getPageIndex()

* searchForm.getPageSize());

List r = clientDao.finds(searchForm);

Map datas = new HashMap();

datas.put("data", r);

datas.put("total", clientDao.totals()==null?0:clientDao.totals());

return datas;

}

/*

* 绉婚櫎瀹㈡埛

*

* @param:瀹㈡埛id

*

* @return:鐘舵��

*/

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

public Msg remove(@RequestParam("id") int id) {

Msg rmMsg = new Msg();

rmMsg.setId(id);

rmMsg.setMsg("刪除成功");

rmMsg.setState(1);

try {

Integer phoneId=null;

phoneId=cellPhoneDao.findByClient(id);

if(null==phoneId){

clientDao.rmove(id);

}else{

rmMsg.setState(0);

rmMsg.setMsg("手机被使用的用户,无法删除。");

}

} catch (Exception e) {

rmMsg.setState(0);

rmMsg.setMsg("手机被使用的用户,无法删除。");

}

return rmMsg;

}

/*

* 淇敼瀹㈡埛淇℃伅

*

* @param:瀹㈡埛淇℃伅

*

* @return:鐘舵�佔刺�

*

* @RequestMapping(value = "client/update", method = RequestMethod.POST)

* public Msg update(@ModelAttribute("client") Client client) {

*

* Msg rmMsg = new Msg(); rmMsg.setId(client.getId());

* rmMsg.setMsg("绉婚櫎鎴愬姛"); rmMsg.setState(1); try {

* clientDao.update(client); } catch (Exception e) { rmMsg.setState(0);

* rmMsg.setMsg("绉婚櫎澶辫触"); } return rmMsg; }

*/

/*

* 娣诲姞瀹㈡埛淇℃伅

*

* @param:瀹㈡埛淇℃伅

*

* @return:鐘舵�佔刺�

*/

@RequestMapping(value = "client/add", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")

public Msg add(@RequestBody Client client) {

logger.debug(client.toString());

Msg rmMsg = new Msg();

rmMsg.setId(client.getId());

rmMsg.setMsg("修改成功");

rmMsg.setState(1);

try {

clientService.add(client);

} catch (Exception e) {

rmMsg.setState(0);

rmMsg.setMsg("客戶已經存在");

}

return rmMsg;

}

}

5. CellPhoneController

package com.green.phonemanage.controller;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.ModelAttribute;

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.RequestParam;

import org.springframework.web.bind.annotation.RestController;

import com.green.phonemanage.dao.CellPhoneDao;

import com.green.phonemanage.model.CellPhone;

import com.green.phonemanage.model.Msg;

import com.green.phonemanage.model.SearchForm;

import com.green.phonemanage.service.IPhoneService;

@RestController

public class CellPhoneController {

@Autowired

private IPhoneService phoneService;

public void setPhoneService(IPhoneService phoneService) {

this.phoneService = phoneService;

}

@Autowired

private CellPhoneDao cellPhoneDao;

public void setCellPhoneDao(CellPhoneDao cellPhoneDao) {

this.cellPhoneDao = cellPhoneDao;

}

/*

* ͨ查找手机

*

* @param:手机id

*

* @return:手机信息

*/

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

public CellPhone findUser(@RequestParam("id") int id) {

return cellPhoneDao.findById(id);

}

/*

* 分页获取手机信息

*

* @param:分页信息

*

* @return:分页所有手机信息

*/

@RequestMapping(value = "cellphone/finds", method = RequestMethod.POST)

public Map finds(SearchForm searchForm) {

searchForm.setPageIndex(searchForm.getPageIndex() * searchForm.getPageSize());

Listr= cellPhoneDao.finds(searchForm);

Map datas = new HashMap();

datas.put("data", r);

datas.put("total", cellPhoneDao.totals()==null?0:cellPhoneDao.totals());

return datas;

}

/*

* 移除手机

*

* @param:手机id

*

* @return:状态

*/

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

public Msg remove(@RequestParam("id") int id) {

Msg rmMsg = new Msg();

rmMsg.setId(id);

rmMsg.setMsg("删除成功");

rmMsg.setState(1);

try {

cellPhoneDao.rmove(id);

} catch (Exception e) {

rmMsg.setState(0);

rmMsg.setMsg("删除失败");

}

return rmMsg;

}

/*

* 修改手機信息

*

* @param:手機信息

*

* @return:状态״̬

*/

@RequestMapping(value = "cellphone/update", method = RequestMethod.POST,consumes="application/json")

public Msg update(@RequestBody CellPhone cellphone) {

Msg rmMsg = new Msg();

rmMsg.setId(cellphone.getId());

rmMsg.setMsg("移除成功");

rmMsg.setState(1);

try {

phoneService.add(cellphone);

} catch (Exception e) {

rmMsg.setState(0);

rmMsg.setMsg("移除失败");

}

return rmMsg;

}

/*

* 添加手机信息

*

* @param:手机信息

*

* @return:状态״̬

*/

@RequestMapping(value = "cellphone/add", method = RequestMethod.POST,consumes="application/json")

public Msg add(@RequestBody CellPhone cellphone) {

cellphone.setStatus(1);

Msg rmMsg = new Msg();

rmMsg.setId(cellphone.getId());

rmMsg.setMsg("添加成功");

rmMsg.setState(1);

try {

phoneService.add(cellphone);

} catch (Exception e) {

rmMsg.setState(0);

rmMsg.setMsg("添加失败");

}

return rmMsg;

}

}

6. MenuController

package com.green.phonemanage.controller;

import java.util.ArrayList;

import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

import com.green.phonemanage.model.MenuItem;

@RestController

public class MenuController {

@RequestMapping(value = "/menu", method = RequestMethod.GET,produces="application/json")

public List menu(int role){

Listitems=new ArrayList();

if(role==1){

MenuItem itemTop=new MenuItem();

itemTop.setId("top");

itemTop.setIconCls("icon-add");

itemTop.setText("系统管理");

items.add(itemTop);

MenuItem itemPhone=new MenuItem();

itemPhone.setId("phone");

itemPhone.setText("手机管理");

itemPhone.setIconCls("Phone");

itemPhone.setUid("top");

itemPhone.setIconPosition("top");

itemPhone.setUrl("web/cellphone/index");

items.add(itemPhone);

MenuItem itemClient=new MenuItem();

itemClient.setId("client");

itemClient.setText("客户管理");

itemClient.setIconCls("Client");

itemClient.setUid("top");

itemClient.setIconPosition("top");

itemClient.setUrl("web/client/index");

items.add(itemClient);

MenuItem itemUser=new MenuItem();

itemUser.setId("user");

itemUser.setText("用户设置");

itemUser.setUid("top");

itemUser.setIconPosition("top");

itemUser.setIconCls("User");

itemUser.setUrl("web/user/edit");

items.add(itemUser);

}

if(role==2){

MenuItem itemTop=new MenuItem();

itemTop.setId("top");

itemTop.setIconCls("icon-add");

itemTop.setText("系统管理");

items.add(itemTop);

MenuItem itemPhone=new MenuItem();

itemPhone.setId("phone");

itemPhone.setText("手机管理");

itemPhone.setIconCls("Phone");

itemPhone.setUid("top");

itemPhone.setIconPosition("top");

itemPhone.setUrl("web/cellphone/index");

items.add(itemPhone);

MenuItem itemClient=new MenuItem();

itemClient.setId("client");

itemClient.setText("客户管理");

itemClient.setIconCls("Client");

itemClient.setUid("top");

itemClient.setIconPosition("top");

itemClient.setUrl("web/client/index");

items.add(itemClient);

MenuItem itemUser=new MenuItem();

itemUser.setId("user");

itemUser.setText("用户管理");

itemUser.setUid("top");

itemUser.setIconPosition("top");

itemUser.setIconCls("User");

itemUser.setUrl("web/user/index");

items.add(itemUser);

MenuItem itemPwd=new MenuItem();

itemPwd.setId("pwd");

itemPwd.setText("修改密码");

itemPwd.setUid("top");

itemPwd.setIconPosition("top");

itemPwd.setIconCls("Pwd");

itemPwd.setUrl("web/user/edit/pwd");

items.add(itemPwd);

}

MenuItem itemTop2=new MenuItem();

itemTop2.setId("top2");

itemTop2.setIconCls("icon-add");

itemTop2.setText("分析");

items.add(itemTop2);

MenuItem itemStatus=new MenuItem();

itemStatus.setId("status");

itemStatus.setText("状态");

itemStatus.setUid("top2");

itemStatus.setIconPosition("left");

itemStatus.setIconCls("status");

itemStatus.setUrl("web/bar/status");

items.add(itemStatus);

MenuItem itemBrand=new MenuItem();

itemBrand.setId("brand");

itemBrand.setText("品牌");

itemBrand.setUid("top2");

itemBrand.setIconPosition("left");

itemBrand.setIconCls("brand");

itemBrand.setUrl("web/bar/brand");

items.add(itemBrand);

MenuItem itemCity=new MenuItem();

itemCity.setId("city");

itemCity.setText("地址");

itemCity.setUid("top2");

itemCity.setIconPosition("left");

itemCity.setIconCls("city");

itemCity.setUrl("web/bar/address");

items.add(itemCity);

return items;

}

7. BarViewController

package com.green.phonemanage.controller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;

@Controller

@RequestMapping("/web/bar/")

public class BarViewController {

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

public ModelAndView toStatus() {

ModelAndView andView = new ModelAndView("/bar/status");

return andView;

}

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

public ModelAndView toAddress() {

ModelAndView andView = new ModelAndView("/bar/address");

return andView;

}

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

public ModelAndView toBrand() {

ModelAndView andView = new ModelAndView("/bar/brand");

return andView;

}

}

8. CellPhoneViewController

package com.green.phonemanage.controller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;

@RequestMapping("/web/cellphone/")

@Controller

public class CellPhoneViewController {

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

public ModelAndView index() {

ModelAndView andView = new ModelAndView("cellphone/index");

return andView;

}

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

public ModelAndView edit() {

ModelAndView andView = new ModelAndView("cellphone/edit");

return andView;

}

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

public ModelAndView aedit() {

ModelAndView andView = new ModelAndView("cellphone/aedit");

return andView;

}

}

9. ClientViewController

package com.green.phonemanage.controller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;

@Controller

@RequestMapping("/web/client")

public class ClientViewController {

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

public ModelAndView index() {

ModelAndView andView = new ModelAndView("client/index");

return andView;

}

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

public ModelAndView edit() {

ModelAndView andView = new ModelAndView("client/edit");

return andView;

}

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

public ModelAndView aedit() {

ModelAndView andView = new ModelAndView("client/eindex");

return andView;

}

}

10. TotalController

package com.green.phonemanage.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

import com.green.phonemanage.model.SearchTotal;

import com.green.phonemanage.service.IPhoneService;

@RestController

public class TotalController {

@Autowired

private IPhoneService phoneService;

public void setPhoneService(IPhoneService phoneService) {

this.phoneService = phoneService;

}

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

public List datas(int type) {

return phoneService.findTotal(type);

}

}

11. UserViewController

package com.green.phonemanage.controller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;

@Controller

@RequestMapping("/web/user/")

public class UserViewController {

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

public ModelAndView index() {

ModelAndView andView = new ModelAndView("/user/index");

return andView;

}

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

public ModelAndView edit() {

ModelAndView andView = new ModelAndView("/user/edit");

return andView;

}

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

public ModelAndView editPwd() {

ModelAndView andView = new ModelAndView("/user/pwdedit");

return andView;

}

}

以上是近期一个简单管理系统SpringMVC用法,其中前端用的是miniui框架。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值