标签:单引号 rest 方法参数 oid 服务器搭建 commons ++ change org
* 1,设置默认值(如果页面传递值为null,自动设置为默认值) @RequestParam(defaultValue="2")* 2,设置别名 (如果页面传递参数名称和方法接受参数名称不一致,设置别名) @RequestParam(value="ids")* 同时设置:* @RequestParam(defaultValue="2",value="ids")
/**
* 需求:根据id更新商品数据
* 请求:/item/updateSubmit
* 参数:item
* 返回值:重定向商品列表
*/
@RequestMapping("/item/updateSubmit")
public String updateSubmit(Item item){
itemService.updateItemByID(item);
return "redirect:/itemsList.do";
}
<!-- 商品更新 -->
<update id="updateItemByID" parameterType="item" >
update items set name=#{name},price=#{price},detail=#{detail},pic=#{pic},createtime=#{createtime}
where id=#{id}
</update>
@RequestMapping("/item/updateSubmit")
public String updateSubmit(Item item){
itemService.updateItemByID(item);
return "redirect:itemsList.do";
}
@Controller
@RequestMapping("for")
public class ForwardController {
@RequestMapping("list")
public String list(){
System.out.println("hhhhhh");
return null;
}
/**
* 需求:转发
* 本类转发:
* 语法:
* 1,return "forward:list.html"
* 2,return "forward:/red/list.html"
* 跨类转发:
* 1,return "forward:/itemsList.html"
*/
public String forward(){
//return "forward:/red/list.html";
//本类转发:
//子路径
//return "forward:list.html";
//return "forward:/for/list.html";
//跨类转发:
return "forward:/itemsList.do";
}
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.js" ></script>
<script type="text/javascript">
function reqJson (){
$.ajax({
type:"post",
url:‘${pageContext.request.contextPath}/reqJson.do‘,
contentType:‘application/json;charset=utf-8‘,
data:‘{"name":"黑妹牙膏","price":"999"}‘,
success:function(data){
//返回json对象
alert(data.name);
}
})
}
</script>
<title>Insert title here</title>
</head>
<body>
<button onclick="reqJson();" >请求json,返回json格式的数据</button>
</body>
</html>
@Controller
public class JsonController {
/**
* 需求:跳转传递参数页面
*/
@RequestMapping("json")
public String json(){
return "reqJson";
}
/**
* 需求:接受json格式参数,返回json对象
* @requestBody:接受json格式参数,自动把json格式参数自动转换对象
* @responseBody:自动把返回对象转换成json格式返回
*/
@RequestMapping("reqJson")
@ResponseBody
public Item reqJson(@RequestBody Item item){
System.out.println(item);
return item;
}
}
public class CustomException extends Exception {
//定义一个字符串变量,封装自定义异常信息
private String message;
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
//构造函数,封装异常信息
public CustomException(String message) {
super(message);
this.message=message;
}
}
public class ExceptionResolve implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest req, HttpServletResponse res,
Object obj, Exception ex) {
CustomException ce=null;
//判断异常信息是否是自定义异常
if (ex instanceof CustomException) {
ce=(CustomException) ex;
}else{
//运行时异常信息
ce = new CustomException("运行时异常,请联系管理员!");
}
ModelAndView mv = new ModelAndView();
//设置异常数据
mv.addObject("ex", ce.getMessage());
mv.setViewName("error/error");
return mv;
}
}
<!-- 上传文件解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name="maxUploadSize" value="10240000" ></property><!-- 单位字节 -->
</bean>
<form id="itemForm" action="${pageContext.request.contextPath}/item/updateSubmit" method="post" >
<td>
<!-- 图片回显 -->
<img id="imgSize1ImgSrc" src="" height="100" width="100" >
<!-- ajax异步上传函数 -->
<input type="file" id="imgSize1File" name="imgSize1File" class="file" onchange="submitImgSize1Upload" >
<!-- 保存数据库图片地址 -->
<input type="hidden" id="imgSize1" name="pic" value="" reg="^.+$" tip="亲,您忘记上传图片了" >
</td>
- </form>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.js" ></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.form.js" ></script>
<script type="text/javascript">
function submitImgSize1Upload(){
var option={
type:‘post‘,
url:‘${pageContext.request.contextPath}/uploadPic.do‘,
dataType:‘text‘,
data:{
fileName:‘imgSize1File‘
},
success:function(data){
//把返回json格式字符串转换json对象
var obj = $.parseJSON(data);
//图片立马回显
$("#imgSize1ImgSrc").attr("src",obj.fullPath);
//图片保存数据地址
$("#imgSize1").val(obj.fullPath);
}
};
//使用ajax异步提交表单
$("#itemForm").ajaxSubmit(option);
}
</script>
public class UploadController {
/**
* 需求:上传文件:avi,xls,doc,jpg
* 参数:String fileName,HttpServletRequest,PrintWriter
* 返回值:void
*/
@RequestMapping("/uploadPic")
public void uploadPic(String fileName,HttpServletRequest request,PrintWriter out){
//把request转换多部件对象
MultipartHttpServletRequest mh = (MultipartHttpServletRequest) request;
//获取文件对象
CommonsMultipartFile cm = (CommonsMultipartFile) mh.getFile(fileName);
//获取文件扩展名
//a、获取文件名称
String originalFilename = cm.getOriginalFilename();
//b、截取文件扩展名
String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
//使用uuid生成文件名称
String fName = UUID.randomUUID().toString();
//上传文件 jersey工具
Client create = Client.create();
//制定上传的服务器路径
WebResource resource = create.resource(Constants.IMAGE_ADDR+"upload/"+fName+extName);
//上传
resource.put(String.class, cm.getBytes());
//图片回显,需要图片地址返回给ajax的回调函数
//返回json格式数据
String fullPath = Constants.IMAGE_ADDR+"upload/"+fName+extName;
//构造一个json格式数据
String result="{\"fullPath\":\""+fullPath+"\"}";
//返回
out.print(result);
}
}
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern><!-- 拦截所有,包括css,js,images -->
</servlet-mapping>
public class Interceptor1 implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("这是第一个拦截器的preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("这是第一个拦截器的postHandle");
}
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("这是第一个拦截器的afterCompletion");
}
}
<!-- 配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.itcast.interceptor.Interceptor1" ></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.itcast.interceptor.Interceptor2" ></bean>
</mvc:interceptor>
</mvc:interceptors>
这是第一个拦截器的preHandle这是第二个拦截器的preHandle这是第一个拦截器的afterCompletion
这是第一个拦截器的preHandle这是第二个拦截器的preHandle这是第二个拦截器的postHandle这是第一个拦截器的postHandle这是第二个拦截器的afterCompletion这是第一个拦截器的afterCompletion
public class LoginInterceptor implements HandlerInterceptor{
//判断用户是否登录
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object obj) throws Exception {
//登录页面,登录请求放行
if (req.getRequestURI().contains("login")) {
//放行
return true;
}
Object user = req.getSession().getAttribute("user");
if (user!=null) {
return true;
}
//如果未登录,跳转登录页面
req.getRequestDispatcher("WEB-INF/jsp/login.jsp").forward(req, res);
return false;
}
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.itcast.interceptor.LoginInterceptor" ></bean>
</mvc:interceptor>
@Controller
public class LoginController {
@RequestMapping("login")
public String login(String username,String password,HttpSession session){
session.setAttribute("user", username);
return "redirect:/itemsList";
}
}
标签:单引号 rest 方法参数 oid 服务器搭建 commons ++ change org
原文地址:http://www.cnblogs.com/qinhelili/p/6984923.html