Java Spring MVC
Spring MVC的实现包括 实现Controller类和基于注解的Controller RequstMapping方式
依赖:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.1.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
Source
基于Controller接口的MVC
这里用到了一个通用接口Controller, Controller的实现类将作为spring的bean被加载到classpath中,在配置文件中定义uri 和bean之间的对应关系。 配置文件的加载在Web.xml中指定 同时Web.xml还负责定义全局的Dispatcher Servlet来接受根 /的 所有请求。
依赖:
1. Controller接口:
Package: org.springframework.web.servlet.mvc;
方法: ModelAndView handleRequest(HttpServletRequest var1, HttpServletResponse var2) throws Exception;
实现:
public class ProductInputController2 implements Controller { // GET 方法 返回表单
/// 这里返回ModelAndView的示例 参数代表JSP的名字
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
return new ModelAndView("ProductForm");
}
} public class ProductSaveController2 implements Controller { public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
productForm form = new productForm(0,httpServletRequest.getParameter("name"), Float.parseFloat(httpServletRequest.getParameter("price")));
product p = new product(form.get_id(),form.get_name(),form.get_price()); // 这里设置request的对象参数。 将来会在ProductDetails的view中用于显示对象属性
return new ModelAndView("ProductDetails","product",p);
}
}
2. 配置文件:
上面定义了两个controller的行为和数据,那么下一步就是定义请求uri和Controler的对应。这里在配置文件中的bean定义来实现
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
/// 对应的Controller此处定义为Bean 对应的Name为path.
<bean name="/product_input.action" class="controller.ProductInputController2"></bean>
<bean name="/product_save.action" class="controller.ProductSaveController2"></bean>
/// View Resolver使的我们在定义ModelAndview()实例的时候不用再指定path和后缀
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
3. 配置文件的加载:
通过Web.xml 中自定义DispatcherServlet的方式来实现。
Servlet: org.springframework.web.servlet.DispatcherServlet 专门用来处理根请求的分发
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"> <servlet>
<servlet-name>controllerServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-web.xml</param-value>
</init-param> <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>controllerServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>
基于注解的MVC
基于注解的MVC主要解决的问题是一个文件定义一个controller(@Controller)多个request uri(@RequestMapping)。
并且uri和handlerRequest的影射关系不再需要在配置文件中指定。 在Controller的方法中就指定就可以了。
当然controller还是作为bean还是要借助配置文件被加载到classpath中的。类似于<context: component-scan />会加载所有controller文件并且是单例的
这里介绍一个例子:
1. Model: 这里定义一个将用于UI 上现实的Model(注意这个 Model的属性名称 必须和UI 上的控件Name对应,这样能实现对象与UI 的绑定)
public class productForm {
private int id;
private String name;
private float price; public float getPrice() {
return price;
} public void setPrice(float price) {
this.price = price;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public productForm(int id, String name, float price){
this.id =id;
this.name =name;
this.price =price; } public productForm(){}
}
2. View: 输入jsp 页面定义了上面Model对应的组建(通过Name属性)。这样在POST 方法提交后可以在Controller方法的参数中获取对象
用于输入的表单
<!DOCTYPE HTML>
<html>
<head>
<title>Add Product Form</title>
<style type="text/css">@import url(css/main.css);</style>
</head>
<body> <div id="global">
<form action="product_save.action" method="post">
<fieldset>
<legend>Add a product</legend>
<p>
<label for="name">Product Name: </label>
<input type="text" id="name" name="name"
tabindex="1">
</p>
<p>
<label for="description">Description: </label>
<input type="text" id="description"
name="description" tabindex="2">
</p>
<p>
<label for="price">Price: </label>
<input type="text" id="price" name="price"
tabindex="3">
</p>
<p id="buttons">
<input id="reset" type="reset" tabindex="4">
<input id="submit" type="submit" tabindex="5"
value="Add Product">
</p>
</fieldset>
</form>
</div>
</body>
</html>
原码
用于确认信息的表单
<!DOCTYPE HTML>
<html>
<head>
<title>Save Product</title>
</head>
<body>
<div id="global">
<h4>The product has been saved.</h4>
<p>
<h5>Details:</h5>
Product Name: ${product.name}<br/>
Price: $${product.price}
</p>
</div>
</body>
</html>
Source
3. Controller:@Contoller注解用于定义Controller bean, 这个属性用于在配置文件的 <context: component-scan />
@RequestMapping()用于定义请求地址和handler之间匹配。 Value=""用于标示请求的uri. Method用于标示请求的方式
GET and POST
import model.product;
import model.productForm; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; /**
* Created by ygshen on 7/18/16.
*/
@Controller
public class ProductController { private static Log logger = LogFactory.getLog(ProductController.class); // http://localhost:8080/springmvc/product_form2.action的时候将访问到这个method
@RequestMapping(value = "/product_form2.action")
public String productForm(){ logger.info("Request the product input form ");
return "ProductForm";
}
// http://localhost:8080/springmvc/product_save.action的时候将访问到这个method
// 这里的参数 productForm能够自动根据输入表丹的属性初始化对象。 Model用于传递对象给下一个View。 类似与Request.setAttribute("product",p).
@RequestMapping(value = "/product_save.action", method = {RequestMethod.POST, RequestMethod.PUT})
public String saveForm(productForm form, Model model){
logger.info("request the product save post method");
logger.info("Product Name: "+ form.getName()+ ". Product price: "+ form.getPrice()); product p = new product(form.getId(),form.getName(),form.getPrice());
model.addAttribute("product",p);
return "ProductDetails";
}
}
4. Controller的发现:
之前使用的<bean></bean>定义一个类是bean ,这里因为有@Controller 的存在已经证明是bean了,需要作的是扫描到他。
<context:component-scan /> 就是这个作用。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="web.controller" /> <mvc:annotation-driven></mvc:annotation-driven>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
4. Controelr配置文件的定义:
web.xml中定义DispactcherServlet处理所有请求,并且Servlet中指定配置文件。 内容就是3中指定的。
Formatter、Converter
1. Converter接口
public interface Converter<S, T> {
T convert(S var1);
}
S是原数据类型,T是转换目标
如 日期输入框输入的都是字符串类型,Converter可以负责自动转换成日期类型 而不必在Controller里用代码判断和转换
public class StringToDateConverter implements Converter<String, Date> {
public Date convert(String dateString) {
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern);
simpleDateFormat.setLenient(false);
return simpleDateFormat.parse(dateString);
}
catch (ParseException exception){
throw new IllegalArgumentException("Invalid date formate"+ datePattern);
}
} private String datePattern; public StringToDateConverter(String _datePattern) {
datePattern=_datePattern;
}
}
<form:errors path="publishTime" cssClass="error"></form:errors>
<label for="publishTime">PublishTime:</label>
<form:input path="publishTime" id="publishTimes" type="text"></form:input>
这里Input输入的是字符串,但是因为Converter的存在会自动实现字符串转成目标(Path=publishTime在Model中的属性被定义成 Date类型)类型。
前提是Converter必须在 configure文件中注册了
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="Formatters.StringToDateConverter">
<constructor-arg type="java.lang.String" value="MM-dd-yyyy"></constructor-arg>
</bean>
</set>
</property>
</bean>
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
2. Formatter: 作用和Converter相同,但是 元数据类型只能是String.
public interface Formatter<T> extends Printer<T>, Parser<T> {
}
另外 Formatter的注册方式如下
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<bean class="Formatters.StringToDateConverter">
<constructor-arg type="java.lang.String" value="MM-dd-yyyy"></constructor-arg>
</bean>
</set>
</property>
</bean>
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
Java Spring MVC的更多相关文章
- 支持Java Spring MVC
Java Spring MVC能很方便在后台返回JSON数据,所以与MiniUI进行数据交互非常简单. 1)后台处理: 在MVC控制器中,可以通过方法参数接收数据,也可以通过Request接收更复杂的 ...
- Java spring mvc多数据源配置
1.首先配置两个数据库<bean id="dataSourceA" class="org.apache.commons.dbcp.BasicDataSource&q ...
- Java Spring mvc 操作 Redis 及 Redis 集群
本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5941953.html 关于 Redis 集群搭建可以参考我的另一篇文章 Redis集群搭建与简单使用 R ...
- java spring mvc restful 上传文件
spring mvc 配置文件 <bean class="com.baiyyy.yfz.core.RestfulHandlerMethodMapping" /> ...
- [Java] Spring MVC 知识点
云图: @Service 用于标注业务层组件. 在 Spring-servlet 配置xml中,component-scan 标签默认情况下自动扫描指定路径下的包(含所有子包),将带有@Compone ...
- 从零开始学 Java - Spring MVC 实现跨域资源 CORS 请求
论职业的重要性 问:为什么所有家长都希望自己的孩子成为公务员? 答:体面.有权.有钱又悠闲. 问:为什么所有家长都希望自己的孩子成为律师或医生? 答:体面.有钱.有技能. 问:为什么所有家长都不怎么知 ...
- Java Spring MVC项目搭建(二)——项目配置
1.站点配置文件web.xml 每一个Spring MVC 项目都必须有一个站点配置文件web.xml,他的主要功能吗....有一位大哥已经整理的很好,我借来了,大家看看: 引用博客地址: http: ...
- Java Spring MVC项目搭建(一)——Spring MVC框架集成
1.Java JDK及Tomcat安装 我这里安装的是JDK 1.8 及 Tomcat 8,安装步骤详见:http://www.cnblogs.com/eczhou/p/6285248.html 2. ...
- java spring mvc 全注解
本人苦逼学生一枚,马上就要毕业,面临找工作,实在是不想离开学校.在老师的教导下学习了spring mvc ,配置文件实在繁琐,因此网上百度学习了spring mvc 全注解方式完成spring的装配工 ...
随机推荐
- TinyURL缩短网址服务 - Blog透视镜
TinyURL是个缩短网址服务的网站,提供1个短网址转向指定到长网址,像是杂志书籍中若有网址太长,也都会用TinyURL来缩短网址,例如本篇文章:http://blog.openyu.org/2014 ...
- Android 设置让EditText不自动获取焦点
在EditText所在的父控件中设置如下属性: android:focusable="true" android:focusableInTouchMode="true&q ...
- Codeforces 486D D. Valid Sets
http://codeforces.com/contest/486/problem/D 题意:给定一棵树,点上有权值,以及d,要求有多少种联通块满足最大值减最小值小于等于d. 思路:枚举i作为最大的点 ...
- Win8/Win8.1都有哪些版本?我该选择哪个?(二)
Windows版本分类比较复杂,下文主要为大家理清Win8/Win8.1的版本种类.如果想了解更多,可以结合<Win7/Win8/Win8.1众多版本,我该选择哪个?>一文来了解. 细数W ...
- Keil C51里关于堆栈指针的处理
Keil C是非常优秀的C51编译器,可能是最好的C51编译器,提供各种优化模式,对变量的优化和地址安排做得非常好.这是用C语言写代码的好处之一,如果用汇编写,得费一大番功夫给各个变量安排内存物理地址 ...
- ANSIC程序到KeilC51的移植心得
摘要:本文讲述了将ANSIC程序移植到KeilC51上应该注意的事项.文章讲述了存储类型.指针类型.重入函数.根据目标系统RAM的分布的段定位和仿真栈设置.函数指针.NULL指针问题.字节顺序.交叉汇 ...
- 《Programming WPF》翻译 第6章 5.我们进行到哪里了?
原文:<Programming WPF>翻译 第6章 5.我们进行到哪里了? WPF提供了资源工具,让我们运用在用户界面中,动态并具有一致性.我们可以在资源字典中存储任意资源,并且可以遍及 ...
- BLOB二进制对象(blob.c/h)
BLOB二进制对象(blob.c/h) 数据结构 struct blob_attr { uint32_t id_len; /** 高1位为extend标志,高7位存储id, * 低24位存储data的 ...
- hihoCoder 1092 : Have Lunch Together
题目大意:小hi和小ho去咖啡厅喝咖啡,咖啡厅可以看作是n * m的矩阵,每个点要么为空,要么被人.障碍物.椅子所占据,小hi和小ho想要找两个相邻的椅子.起初两个人都在同一个点,求两人到达满足要求的 ...
- Find Median from Data Stream 解答
Question Median is the middle value in an ordered integer list. If the size of the list is even, the ...