/**
* <p>项目名称:restful
* <p>Package名称:com.hnust.controller
* 文件名称:ResourceController.java
* 版本:1.00
* 创建日期:2014年10月26日
* Copyright©2014 HNUST .All Rights Reserved.
*/
package com.hnust.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hnust.bean.Message;
import com.hnust.bean.Resource;
import com.hnust.service.ResourceService;
/**
*
*@author:Heweipo
*@version 1.00
*
*/
@Controller
@RequestMapping("/resource")
public class ResourceController {
@Autowired
private ResourceService service;
/**
*
* <p>通过id获取资源
* 在这里采用@PathVariable注解,同时返回一个String,让SpringMVC自动寻找最合适的视图解析器
* 优点是能够找到html视图解析器,但是缺点是,JavaBean转为XML格式,需要在 org.springframework.oxm.jaxb.Jaxb2Marshaller绑定JavaBean
* 所以 这里Resource一定要进行绑定
* @param id
* @param model
* @return
* @return String
* author: Heweipo
*/
@RequestMapping(value="/get/{id}" , method=RequestMethod.GET)
public String get(@PathVariable("id") String id , ModelMap model){
model.put("resource", service.getResource(id));
return "resource";
}
/**
*
* <p>存储resource,另外,关于resource的number参数注入很值得注意,如果URL中携带的参数不能转为
* Integer类型,那么是无法继续访问这个方法的!!所以,对于JavaBean最好设置与数据库对应的数据类型
* 进行类型约束。
* 在这里采用@ResponseBody注解,那么返回的对象将会被对应的视图解析器解析,但是不会有html解析器,但是有一个
* 优点:javaBean 如果转为 XML 是不需要在spring-servlet 中 org.springframework.oxm.jaxb.Jaxb2Marshaller 绑定的
* @param resource
* @param model
* @return void
* author: Heweipo
*/
@RequestMapping(value="/put" , method=RequestMethod.PUT)
public @ResponseBody Message put(Resource resource , ModelMap model){
Message message = new Message();
if(StringUtils.isEmpty(resource.getId()) || StringUtils.isEmpty(resource.getName())){
message.setMsg("请输入id 和 用户名");
message.setResult("faile");
return message;
}
service.insertResource(resource);
message.setMsg("数据已经存储");
message.setResult("success");
return message;
}
/**
*
* <p>通过id更新resource
* @param resource
* @param model
* @return
* @return Message
* author: Heweipo
*/
@RequestMapping(value="/post" , method=RequestMethod.POST)
public @ResponseBody Message post(Resource resource , ModelMap model){
Message message = new Message();
if(StringUtils.isEmpty(resource.getId())){
message.setMsg("id不能为空");
message.setResult("faile");
return message;
}
service.updateResource(resource);
message.setMsg("数据已经修改");
message.setResult("success");
return message;
}
/**
*
* <p>通过id删除资源
* @param id
* @param model
* @return
* @return Message
* author: Heweipo
*/
@RequestMapping(value="/delete/{id}" , method=RequestMethod.DELETE)
public @ResponseBody Message delete(@PathVariable("id")String id, ModelMap model){
Message message = new Message();
service.deleteResource(id);
message.setMsg("数据已删除");
message.setResult("success");
return message;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
restful.rar (34个子文件)
restful
pom.xml 2KB
target
test-classes
m2e-wtp
web-resources
META-INF
maven
com.hnust
restful
pom.xml 2KB
pom.properties 220B
MANIFEST.MF 114B
classes
com
hnust
interceptor
TranslateInterceptor.class 1KB
bean
Resource.class 1KB
Message.class 1KB
controller
ResourceController.class 3KB
service
ResourceRepository.class 2KB
ResourceService.class 1KB
spring-servlet.xml 5KB
applicationContext.xml 1KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.core.resources.prefs 74B
org.eclipse.wst.common.component 748B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.validation.prefs 50B
.jsdtscope 508B
org.eclipse.jdt.core.prefs 430B
src
test
java
main
webapp
WEB-INF
web.xml 2KB
jsp
resource.jsp 638B
index.jsp 263B
resource
css
image
plugins
js
resources
spring-servlet.xml 5KB
applicationContext.xml 1KB
java
com
hnust
interceptor
TranslateInterceptor.java 2KB
bean
Resource.java 2KB
Message.java 2KB
controller
ResourceController.java 4KB
service
ResourceRepository.java 2KB
ResourceService.java 2KB
.project 1KB
.classpath 1KB
共 34 条
- 1
wp562846864
- 粉丝: 6
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页