GVKun编程网logo

Tiles 3.0和Spring MVC设置(spring mvc dispatcherservlet)

32

本文的目的是介绍Tiles3.0和SpringMVC设置的详细情况,特别关注springmvcdispatcherservlet的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个

本文的目的是介绍Tiles 3.0和Spring MVC设置的详细情况,特别关注spring mvc dispatcherservlet的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解Tiles 3.0和Spring MVC设置的机会,同时也不会遗漏关于Apache Tiles和Spring MVC中的全局异常页面、java – Spring MVC设置全局DateTimeFormat、JavaEE_Mybatis_SpringMVC_SpringMVC_lesson1_简单的SpringMVC框架搭建指南、JavaEE——SpringMVC(13)--SpringMVC运行流程 在 Spring 的环境下使用 SpringMVC的知识。

本文目录一览:

Tiles 3.0和Spring MVC设置(spring mvc dispatcherservlet)

Tiles 3.0和Spring MVC设置(spring mvc dispatcherservlet)

可以在Spring 3.0中运行Tiles 3.0吗?

需要什么配置?

答案1

小编典典

当前版本的Spring仅正式支持Tiles 2。

为了使Tiles 3正常工作,您需要实现自己的View类,Tiles
Configurer(如果需要)等。Spring社区会非常感激这一点,但是我敢肯定这并非易事。

希望这可以帮助。

Apache Tiles和Spring MVC中的全局异常页面

Apache Tiles和Spring MVC中的全局异常页面

当引发未处理的异常(例如RuntimeException)时,我想显示一个常见的错误页面。

我想实现一些目标:

  • 重复使用HTML模板(使用带有标题等的通用“框架”)并将异常信息放在正文中
  • 在正文文档中提供有关该异常的一些基本信息

我正在使用Apache Tiles和Spring MVC。什么是解决我的问题的好方法?

我的图块定义的一部分:

<tiles-definitions>    <definition name="common" template="/WEB-INF/jsp/common.jsp">        <put-attribute name="header" value="header"/>        <put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp"/>    </definition>   ...   <definition name="main" extends="common">       <put-attribute name="body" value="/WEB-INF/jsp/main.jsp"/>   </definition></tiles-definitions>

理想情况下,我想通过设置body属性为异常页面指定定义…

答案1

小编典典

假设您已经安装了Spring TilesViewResolver并进行了TilesConfigurer配置,则可以尝试以下bean定义:

<bean id="exceptionResolver">   <property name="exceptionMappings">      <props>         <prop key="java.lang.Throwable">error</prop>      </props>   </property></bean>

然后简单地定义逻辑视图error

<definition name="error" extends="common">    <put-attribute name="body" value="/WEB-INF/jsp/error.jsp"/></definition>

这会将任何内容转发Throwable到正确的视图,您可以在其中访问异常本身(${exception})。这不会取代所有标准的HTTP错误页面(针对404等)

java – Spring MVC设置全局DateTimeFormat

java – Spring MVC设置全局DateTimeFormat

我希望我的控制器根据标准格式解析RequestParams和PathVariables中的日期.

是否可以在不单独注释每个@RequestParam的情况下设置应用程序范围的@DateTimeFormat?

最佳答案
您可以在控制器级别执行此操作.在控制器中添加@initBinder,它将根据给定的格式化程序格式化日期.

@InitBinder
public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(false);

    // true passed to CustomDateEditor constructor means convert empty String to null
    binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,true));
}

JavaEE_Mybatis_SpringMVC_SpringMVC_lesson1_简单的SpringMVC框架搭建指南

JavaEE_Mybatis_SpringMVC_SpringMVC_lesson1_简单的SpringMVC框架搭建指南

单独的Spring框架搭建


所需文件

1. SpringMVC框架的包  注意包括JSTL包,SpringMVC需要

如图





2. web.xml 配置文件

里面配置了前端控制器RequestDispatcher,并指定了其他的 处理器Handler,处理器适配器:  处理器映射器  视图解析器的配置文件的地址


3.springmvc.xml 配置文件

 配置了 处理器Handler,处理器适配器:  处理器映射器  视图解析器的配置文件。


4.Handler 的实现类

/SpringMVC_test1/src/cn/itcast/ssm/controller/ItemsController1.java


5.自定义的pojo,(与Mybatis 整合后,由Mybatis 提供),由Mybatis 实现持久层

   为 Items.java


6.前端的JSP 测试页面

/SpringMVC_test1/WebRoot/WEB-INF/jsp/items/itemsList.jsp


整体的文档目录图如下图所示:




注意事项

(1)  web.xml 需要放到WEB-INF目录下的第一级目录,否则不会被加载



2. web.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringMVC_test1</display-name>
  
  <!-- springmvc前端控制器 -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等)
  	如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-serlvet.xml(springmvc-servlet.xml)
  	 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<!--
  	第一种: *.action, 访问以action结尾 由DispatcherServlet进行解析
  	第二种:/,所有的访问地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不让
  	DispatcherServlet进行解析,使用此方式可以实现RESTful风格的url 
  	第三种: /*,这样配置不对,使用这种配置,最终需要转发到一个jsp页面时,仍然会由
  	DispatcherServlet解析jsp,不能根据jsp页面找到handler,会报错。
  	 -->
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

3.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"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">


	<!-- 处理器Handler: 处理器映射器映射的具体的处理器 -->
	<bean name="/queryItems.action" class="cn.itcast.ssm.controller.ItemsController1"/>

	<!-- 处理器适配器:  所有处理器适配器都实现 HandlerAdapter接口 -->
	<bean
		class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

	<!-- 处理器映射器 :	将Bean的 name的 url进行查找,需要在配置Handler指定Beanname(就是url) -->
	<bean
		class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />


	<!-- 视图解析器 -->
	<!-- ViewResolver
	 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	</bean>
</beans>


4.Handler 的实现类

/SpringMVC_test1/src/cn/itcast/ssm/controller/ItemsController1.java

package cn.itcast.ssm.controller;
  • import java.util.ArrayList;
  • import java.util.List;
  • import javax.servlet.http.HttpServletRequest;
  • import javax.servlet.http.HttpServletResponse;
  • import org.springframework.web.servlet.ModelAndView;
  • import org.springframework.web.servlet.mvc.Controller;
  • import cn.itcast.ssm.po.Items;
  • public class ItemsController1 implements Controller {
  • @Override
  • public ModelAndView handleRequest(HttpServletRequest request,
  • HttpServletResponse response) throws Exception {
  • List<Items> itemsList = new ArrayList<>();
  • Items items_1 = new Items();
  • items_1.setName("联想笔记本");
  • items_1.setPrice(6000f);
  • items_1.setDetail("ThinkPad T430 联想笔记本电脑!");
  • Items items_2 = new Items();
  • items_2.setName("苹果手机");
  • items_2.setPrice(5000f);
  • items_2.setDetail("iphone6苹果手机!");
  • itemsList.add(items_1);
  • itemsList.add(items_2);
  • // 返回ModelAndView
  • ModelAndView modelAndView = new ModelAndView();
  • // 相当于request 的 setAttribute, 在 jsp 页面中通过 itemList 取数据
  • modelAndView.addObject("itemsList", itemsList);
  • // 指定视图
  • modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
  • return modelAndView;
  • }
  • }


  • 5.自定义的pojo,(与Mybatis 整合后,由Mybatis 提供),由Mybatis 实现持久层

       为 Items.java

    1. package cn.itcast.ssm.po;
    2. import java.util.Date;
    3. public class Items {
    4. private Integer id;
    5. private String name;
    6. private Float price;
    7. private String pic;
    8. private Date createtime;
    9. private String detail;
    10. public Integer getId() {
    11. return id;
    12. }
    13. public void setId(Integer id) {
    14. this.id = id;
    15. }
    16. public String getName() {
    17. return name;
    18. }
    19. public void setName(String name) {
    20. this.name = name == null ? null : name.trim();
    21. }
    22. public Float getPrice() {
    23. return price;
    24. }
    25. public void setPrice(Float price) {
    26. this.price = price;
    27. }
    28. public String getPic() {
    29. return pic;
    30. }
    31. public void setPic(String pic) {
    32. this.pic = pic == null ? null : pic.trim();
    33. }
    34. public Date getCreatetime() {
    35. return createtime;
    36. }
    37. public void setCreatetime(Date createtime) {
    38. this.createtime = createtime;
    39. }
    40. public String getDetail() {
    41. return detail;
    42. }
    43. public void setDetail(String detail) {
    44. this.detail = detail == null ? null : detail.trim();
    45. }
    46. }


    6.前端的JSP 测试页面

    /SpringMVC_test1/WebRoot/WEB-INF/jsp/items/itemsList.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
    <!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>查询商品列表</title>
    </head>
    <body>
    	<form
    		action="${pageContext.request.contextPath }/item/queryItem.action"
    		method="post">
    		查询条件:
    		<table width="100%" border=1>
    			<tr>
    				<td><input type="submit" value="查询" /></td>
    			</tr>
    		</table>
    		商品列表:
    		<table width="100%" border=1>
    			<tr>
    				<td>商品名称</td>
    				<td>商品价格</td>
    				<td>生产日期</td>
    				<td>商品描述</td>
    				<td>操作</td>
    			</tr>
    			<c:forEach items="${itemsList }" var="item">
    				<tr>
    					<td>${item.name }</td>
    					<td>${item.price }</td>
    					<td><fmt:formatDate value="${item.createtime}"
    							pattern="yyyy-MM-dd HH:mm:ss" /></td>
    					<td>${item.detail }</td>
    
    					<td><a
    						href="${pageContext.request.contextPath }/item/editItem.action?id=${item.id}">修改</a></td>
    
    				</tr>
    			</c:forEach>
    
    		</table>
    	</form>
    </body>
    
    </html>

    测试结果:显示


    JavaEE——SpringMVC(13)--SpringMVC运行流程 在 Spring 的环境下使用 SpringMVC

    JavaEE——SpringMVC(13)--SpringMVC运行流程 在 Spring 的环境下使用 SpringMVC

    需要进行 Spring 整合 SpringMVC 吗 ?
    还是否需要再加入 Spring 的 IOC 容器 ?
    是否需要再 web.xml 文件中配置启动 Spring IOC 容器的 ContextLoaderListener ?
    spring的IOC

    package com.atguigu.springmvc;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserService {
    
    	@Autowired
    	private HelloWorld helloWorld;
    	
    	public UserService() {
    		System.out.println("UserService Constructor...");
    	}
    	
    }

      

    SpringMVC的IOC

    package com.atguigu.springmvc;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class HelloWorld {
    
    	@Autowired
    	private UserService userService;
    	
    	public HelloWorld() {
    		System.out.println("HelloWorld Constructor...");
    	}
    	
    	@RequestMapping("/helloworld")
    	public String hello(){
    		System.out.println("success");
    		System.out.println(userService);
    		return "success";
    	}
    	
    }

      

    1. 需要: 通常情况下, 类似于数据源, 事务, 整合其他框架都是放在 Spring 的配置文件中(而不是放在 SpringMVC 的配置文件中).
    实际上放入 Spring 配置文件对应的 IOC 容器中的还有 Service 和 Dao.
    2. 不需要: 都放在 SpringMVC 的配置文件中. 也可以分多个 Spring 的配置文件, 然后使用 import 节点导入其他的配置文件

    问题: 若 Spring 的 IOC 容器和 SpringMVC 的 IOC 容器扫描的包有重合的部分, 就会导致有的 bean 会被创建 2 次.
    解决:
      1. 使 Spring 的 IOC 容器扫描的包和 SpringMVC 的 IOC 容器扫描的包没有重合的部分.
      *2. 使用 exclude-filter 和 include-filter 子节点来规定只能扫描的注解

    springmvc.xml

    <context:component-scan base-package="com.atguigu.springmvc" use-default-filters="false">
    		<context:include-filter type="annotation" 
    			expression="org.springframework.stereotype.Controller"/>
    		<context:include-filter type="annotation" 
    			expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    	</context:component-scan>

      

    bean.xml

    <context:component-scan base-package="com.atguigu.springmvc">
    		<context:exclude-filter type="annotation" 
    			expression="org.springframework.stereotype.Controller"/>
    		<context:exclude-filter type="annotation" 
    			expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    	</context:component-scan>

      

    SpringMVC 的 IOC 容器中的 bean 可以来引用 Spring IOC 容器中的 bean.
    返回来呢 ? 反之则不行. Spring IOC 容器中的 bean 却不能来引用 SpringMVC IOC 容器中的 bean!

     

    今天关于Tiles 3.0和Spring MVC设置spring mvc dispatcherservlet的讲解已经结束,谢谢您的阅读,如果想了解更多关于Apache Tiles和Spring MVC中的全局异常页面、java – Spring MVC设置全局DateTimeFormat、JavaEE_Mybatis_SpringMVC_SpringMVC_lesson1_简单的SpringMVC框架搭建指南、JavaEE——SpringMVC(13)--SpringMVC运行流程 在 Spring 的环境下使用 SpringMVC的相关知识,请在本站搜索。

    本文标签:

    上一篇如何在MultiActionController中执行Spring验证?(multiple spring)

    下一篇Spring MVC应用程序-如何设置会话范围的Bean值(springmvc设置session有效期)