本文共 4999 字,大约阅读时间需要 16 分钟。
本次实现数据的CRUD功能,数据依然以VO类形式进行数据接收。
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 + "]"; }} 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=天天新闻
后台服务器结果如下:
以上地址组成结构如下:
此时的代码最大特点是,控制器不需要编写类属性接收参数,所有的接收参数放到了处理的业务方法上。
同时避免了的实例化对象问题。
而整个操作过程中最为关键的问题是:传递的参数只需要传递属性名称即可。如果是引用的关系,则只需要按照“.”排列即可(如上面的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页面。这个页面的功能是进行操作功能完成后的信息提示。
<%@ 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页面,至少需要以下内容:
正因为如此,在springMVC中专门设计了一个类:ModelAndView,而这个类里面定义了如下操作方法:
范例:更好的处理跳转
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/