博客
关于我
20-spring学习-Spring MVC基本操作
阅读量:796 次
发布时间:2023-03-24

本文共 4999 字,大约阅读时间需要 16 分钟。

数据CRUD实现与SpringMVC的应用

本次实现数据的CRUD功能,数据依然以VO类形式进行数据接收。

一、建立Message.java类操作,负责数据的接收操作

package com.SpringMVC.vo;
public class Type {
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "Type [title=" + title + "]";
}
}
package com.SpringMVC.vo;
import java.io.Serializable;
import java.util.Date;
@SuppressWarnings("serial")
public class Message implements Serializable {
private Integer mid;
private String title;
private Double price;
private Date pubdate;
private Type type;
public Integer getMid() {
return mid;
}
public void setMid(Integer mid) {
this.mid = mid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Date getPubdate() {
return pubdate;
}
public void setPubdate(Date pubdate) {
this.pubdate = pubdate;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
@Override
public String toString() {
return "Message [mid=" + mid + ", title=" + title + ", price=" + price + ", pubdate=" + pubdate + ", type=" + type + "]";
}
}

二、定义Action

package com.SpringMVC.action;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.SpringMVC.vo.Message;
@Controller
@RequestMapping("/pages/back/message/*")
public class MessageAction {
@RequestMapping("hello_demo")
public void demo(Message msg) {
System.out.println(msg);
}
}

下面由于第一次执行,可以直接利用地址重写方式传递所需要数据。

http://localhost:8080/SpringMVC/pages/back/message/hello_demo.action?mid=12&title=今日头条&price=8&type.title=天天新闻

后台服务器结果如下:

以上地址组成结构如下:

  • action的父路径:http://localhost:8080/SpringMVC/pages/back/message/
  • 配置的方法路径:/hello_demo.action
  • 表示Message对象的组成:?mid=12&title=今日头条&price=8&type.title=天天新闻
  • 此时的代码最大特点是,控制器不需要编写类属性接收参数,所有的接收参数放到了处理的业务方法上。

    同时避免了的实例化对象问题。

    而整个操作过程中最为关键的问题是:传递的参数只需要传递属性名称即可。如果是引用的关系,则只需要按照“.”排列即可(如上面的type.title)。

    在springMVC中还可以设置某一个业务方法的请求类型。如对之前的action进行修改:

    package com.SpringMVC.action;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import com.SpringMVC.vo.Message;
    @Controller
    @RequestMapping("/pages/back/message/*")
    public class MessageAction {
    @RequestMapping(value="hello_demo", method=RequestMethod.GET)
    public void demo(Message msg) {
    System.out.println(msg);
    }
    }

    如果使用这种语法表示配置的方法只能使用get请求模式进行触发。也可以修改为post请求:

    package com.SpringMVC.action;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import com.SpringMVC.vo.Message;
    @Controller
    @RequestMapping("/pages/back/message/*")
    public class MessageAction {
    @RequestMapping(value="hello_demo", method=RequestMethod.POST)
    public void demo(Message msg) {
    System.out.println(msg);
    }
    }

    如果业务处理方法设置为POST请求,那么就表示只能怪由表单提交到此方法上。

    对于返回值的处理,Spring MVC有一些要求,在正常开发中,往往会提供一共forword.jsp页面。这个页面的功能是进行操作功能完成后的信息提示。

    定义forword.jsp页面

    <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1" %>
    <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
    My JSP 'forword.jsp' starting page

    如果由一共控制器,跳转到forword.jsp页面,至少需要以下内容:

  • 控制器需要知道forword.jsp页面的路径。
  • 控制器需要传递若干个request属性(如上面的$(msg)和$(url))。
  • 正因为如此,在springMVC中专门设计了一个类:ModelAndView,而这个类里面定义了如下操作方法:

  • 构造方法:public ModelAndView()。
  • 构造方法:public ModelAndView(String viewName),viewName,跳转的路径地址;
  • 保存属性:public ModelAndView addObject(String attrivbuteName,Object attrivateValue);
  • 范例:更好的处理跳转

    package com.SpringMVC.action;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.portlet.ModelAndView;
    import com.SpringMVC.vo.Message;
    @Controller
    @RequestMapping("/pages/back/message/*")
    public class MessageAction {
    @RequestMapping(value="hello_demo")
    public ModelAndView demo(Message msg) {
    ModelAndView md=new ModelAndView("/Pages/forword.jsp");
    md.addObject("msg","消息信息添加成功");
    md.addObject("url","/index.jsp");
    System.out.println(msg);
    return md;
    }
    }

    这里通过ModelAndView ,跳转到forword.jsp页面,同时,给这个页面传递两个属性值,msg和url。

    然后跳到了下一页

    通过以上程序分析就可以总结出MVC设计的优势:

  • 避免了复杂的路径跳转的配置操作。
  • 避免了项目中出现过多的“.”作为参数的情况。
  • 转载地址:http://fwhfk.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现求中位数(附完整源码)
    查看>>
    Objective-C实现求众数(附完整源码)
    查看>>
    Objective-C实现求圆锥的体积(附完整源码)
    查看>>
    Objective-C实现求曲线在某点的导数(附完整源码)
    查看>>
    Objective-C实现求最大公约数 (GCD)的算法(附完整源码)
    查看>>
    Objective-C实现求梯形面积公式(附完整源码)
    查看>>
    Objective-C实现求模逆算法(附完整源码)
    查看>>
    Objective-C实现求正弦(附完整源码)
    查看>>
    Objective-C实现求矩阵对角线元素之和(附完整源码)
    查看>>
    Objective-C实现汉密尔顿循环算法(附完整源码)
    查看>>
    Objective-C实现波利比乌斯密码算法(附完整源码)
    查看>>
    Objective-C实现波雷费密码算法(附完整源码)
    查看>>
    Objective-C实现洗牌移位密码算法(附完整源码)
    查看>>
    Objective-C实现测试信用卡号码有效性credit card validator的算法(附完整源码)
    查看>>
    Objective-C实现海伦公式(附完整源码)
    查看>>
    Objective-C实现海伦公式(附完整源码)
    查看>>
    Objective-C实现消息队列(附完整源码)
    查看>>
    Objective-C实现消息队列(附完整源码)
    查看>>
    Objective-C实现深度优先搜索迭代算法(附完整源码)
    查看>>
    Objective-C实现深度优先搜索递归算法(附完整源码)
    查看>>