SpringMVC 配置之浅谈传统的非注解控制器
2017-04-30 17:29
323 查看
如上一篇文章,springMVC 的整个运行周期都是 DispatcherServlet 在调度,所以可想而知的重要性,首先在 WEB.XML
中配置好 DispatcherServlet 。
如下(<init-param> 的配置是加载springMVC 的配置文件,需要注意的是如果是默认的xxx-servlet.xml 格式的命名就无需配置 init-param 了):
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
然后配置 springMVC 的 xml,如下,标签解释有注释。
<?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: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-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<description>Spring MVC Configuration</description>
<!-- 加载配置属性文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:springmvcdemo.properties" />
<!-- 定义无Controller的path<->view直接映射 -->
<mvc:view-controller path="/" view-name="redirect:${web.view.index}"/>
<!-- 使用传统风格的控制器 foxa start -->
<bean name="/foxa_input.action" class="com.etfox.test.foxa.controller.InputProductController" />
<bean name="/foxa_save.action" class="com.etfox.test.foxa.controller.SaveProductController" />
<!-- 使用传统风格的控制器 foxa end -->
</beans>
注意,上面配置问津中使用了属性配置文件 properties,这是把一些需要更改的信息加以统一管理,也可以不用,不需要的时候把加载配置文件注掉就行,下面贴出属性文件:
#\u89C6\u56FE\u6587\u4EF6\u5B58\u653E\u8DEF\u5F84
web.view.prefix=/WEB-INF/jsp/
web.view.suffix=.jsp
#\u521D\u59CB\u8BF7\u6C42\u8DEF\u5F84
#web.view.index=/foxa_input.action
web.view.index=/foxb_input
正如springMVC 的配置文件,加载的就是属性文件中的值。
其中使用传统控制器配置的跳转 bean , 传统控制器需要实现 Controller ,如此相较于使用注解的方式来说一个控制器只能处理一个请求,而使用注解的方式可以一个控制器
实现多个 requestmapping 处理,减少了代码量,并且无需实现 controller ,减少了和 springmvc 的耦合度,更符合轻量级的设计理念。话不多说:
/**
* Copyright ©2017 DarkFOX. All rights reserved.
*
* @Title: InputProductController.java
* @Prject: SpringMvcDemo
* @Package: com.bwh.test.x00a.controller
* @Description: TODO
* @author: ETFox
* @date: 2017年4月25日 上午9:51:32
* @version: V1.0
*/
package com.etfox.test.foxa.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
/**
* @ClassName: InputProductController
* @Description: TODO 使用传统风格的控制器
* @author: ETFox
* @date: 2017年4月25日 上午9:51:32
*/
public class InputProductController implements Controller {
private static final Log logger = LogFactory.getLog(InputProductController.class);
/**
* @Title: handleRequest
* @Description: TODO 此方法包含一个视图,且没有模型。因此,该请求被转发到指定的 URL
* @param request
* @param response
* @return
* @throws Exception
* @see org.springframework.web.servlet.mvc.Controller#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
logger.info("InputProductController called");
return new ModelAndView("/WEB-INF/jsp/foxa/ProductForm.jsp");
}
}
JSP 页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ProductForm</title>
</head>
<body>
<form action="foxa_save" method="post">
<fieldset>
<legend>Add Product Form</legend>
<label for="name">product name:</label>
<input type="text" id="name" value="" tabindex="1" />
<label for="price">price:</label>
<input type="text" id="price" value="" tabindex="2" />
<div id="buttons">
<label for="dummy"> </label>
<input type="reset" id="reset" tabindex="3" />
<input type="submit" value="Add Product" tabindex="4" />
</div>
</fieldset>
</form>
</body>
</html>
如上 SaveProductController 中返回的 ModelAndView("/WEB-INF/jsp/foxa/ProductDetails.jsp", "product", product); 包含了视图和模型,其中模型包含我们返回view 的数据,springMVC 会将数据存在 request 域中。
中配置好 DispatcherServlet 。
如下(<init-param> 的配置是加载springMVC 的配置文件,需要注意的是如果是默认的xxx-servlet.xml 格式的命名就无需配置 init-param 了):
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
然后配置 springMVC 的 xml,如下,标签解释有注释。
<?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: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-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<description>Spring MVC Configuration</description>
<!-- 加载配置属性文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:springmvcdemo.properties" />
<!-- 定义无Controller的path<->view直接映射 -->
<mvc:view-controller path="/" view-name="redirect:${web.view.index}"/>
<!-- 使用传统风格的控制器 foxa start -->
<bean name="/foxa_input.action" class="com.etfox.test.foxa.controller.InputProductController" />
<bean name="/foxa_save.action" class="com.etfox.test.foxa.controller.SaveProductController" />
<!-- 使用传统风格的控制器 foxa end -->
</beans>
注意,上面配置问津中使用了属性配置文件 properties,这是把一些需要更改的信息加以统一管理,也可以不用,不需要的时候把加载配置文件注掉就行,下面贴出属性文件:
#\u89C6\u56FE\u6587\u4EF6\u5B58\u653E\u8DEF\u5F84
web.view.prefix=/WEB-INF/jsp/
web.view.suffix=.jsp
#\u521D\u59CB\u8BF7\u6C42\u8DEF\u5F84
#web.view.index=/foxa_input.action
web.view.index=/foxb_input
正如springMVC 的配置文件,加载的就是属性文件中的值。
其中使用传统控制器配置的跳转 bean , 传统控制器需要实现 Controller ,如此相较于使用注解的方式来说一个控制器只能处理一个请求,而使用注解的方式可以一个控制器
实现多个 requestmapping 处理,减少了代码量,并且无需实现 controller ,减少了和 springmvc 的耦合度,更符合轻量级的设计理念。话不多说:
/**
* Copyright ©2017 DarkFOX. All rights reserved.
*
* @Title: InputProductController.java
* @Prject: SpringMvcDemo
* @Package: com.bwh.test.x00a.controller
* @Description: TODO
* @author: ETFox
* @date: 2017年4月25日 上午9:51:32
* @version: V1.0
*/
package com.etfox.test.foxa.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
/**
* @ClassName: InputProductController
* @Description: TODO 使用传统风格的控制器
* @author: ETFox
* @date: 2017年4月25日 上午9:51:32
*/
public class InputProductController implements Controller {
private static final Log logger = LogFactory.getLog(InputProductController.class);
/**
* @Title: handleRequest
* @Description: TODO 此方法包含一个视图,且没有模型。因此,该请求被转发到指定的 URL
* @param request
* @param response
* @return
* @throws Exception
* @see org.springframework.web.servlet.mvc.Controller#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
logger.info("InputProductController called");
return new ModelAndView("/WEB-INF/jsp/foxa/ProductForm.jsp");
}
}
/** * Copyright ©2017 DarkFOX. All rights reserved. * * @Title: SaveProductController.java * @Prject: SpringMvcDemo * @Package: com.bwh.test.x00a.controller * @Description: TODO * @author: ETFox * @date: 2017年4月25日 上午9:51:42 * @version: V1.0 */ package com.etfox.test.foxa.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import com.etfox.test.foxa.model.Product; /** * @ClassName: SaveProductController * @Description: TODO 使用传统风格的控制器 * @author: ETFox * @date: 2017年4月25日 上午9:51:42 */ public class SaveProductController implements Controller { private static final Log logger = LogFactory.getLog(InputProductController.class); /** * @Title: handleRequest * @Description: TODO 此方法包含路径、模型 * @param request * @param response * @return * @throws Exception * @see org.springframework.web.servlet.mvc.Controller#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub Product product = new Product(); product.setName(request.getParameter("name")); product.setPrice(Double.valueOf(request.getParameter("price"))); logger.info("SaveProductController called"); return new ModelAndView("/WEB-INF/jsp/foxa/ProductDetails.jsp", "product", product); } }
JSP 页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ProductForm</title>
</head>
<body>
<form action="foxa_save" method="post">
<fieldset>
<legend>Add Product Form</legend>
<label for="name">product name:</label>
<input type="text" id="name" value="" tabindex="1" />
<label for="price">price:</label>
<input type="text" id="price" value="" tabindex="2" />
<div id="buttons">
<label for="dummy"> </label>
<input type="reset" id="reset" tabindex="3" />
<input type="submit" value="Add Product" tabindex="4" />
</div>
</fieldset>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>ProductDetails</title> </head> <body> <fieldset> <legend>Details</legend> <h5>===========</h5> ${product.name }<br/> $${product.price } <h5>===========</h5> </fieldset> </body> </html>
如上 SaveProductController 中返回的 ModelAndView("/WEB-INF/jsp/foxa/ProductDetails.jsp", "product", product); 包含了视图和模型,其中模型包含我们返回view 的数据,springMVC 会将数据存在 request 域中。
相关文章推荐
- SpringMVC非注解一个控制器配置多个方法
- spring 基于注解的控制器配置
- java 中 springmvc + openjpa 配置 注解
- 基于注解的Spring MVC整合Hibernate(所需jar包,spring和Hibernate整合配置,springMVC配置,重定向,批量删除)
- springMVC学习笔记六(注解方式实现控制器)
- 配置SpringMVC注解遇到的一些小问题
- springMVC 注解事务配置
- 缓存初解(五)---SpringMVC基于注解的缓存配置--web应用实例
- SpringMvc的视图解析器与核心控制器配置以及使用Freemarker模板的配置
- springmvc注解配置详解及其demo代码下载
- SpringMVC学习笔记(2) 框架配置+HelloWorld(注解版)
- springMVC基于注解配置教程一
- SpringMVC 多控制器,静态资源访问,配置文件位置更改
- 浅谈spring——注解配置(九)
- 秀外慧中的springMVC(二)---springMVC的注解配置例子
- SpringMVC之用注解控制器
- 浅谈spring——注解配置(九)
- SpringMvc简单注解配置
- springmvc梳理7---注解控制器
- SpringMVC之用注解控制器