手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

使用Eclipse构建Maven的SpringMVC项目

[日期:2012-09-16] 来源:Linux社区  作者:limingnihao [字体:大 中 小]

4、构建SpringMVC框架

4.1 编辑web.xml文件

需要添加log4j,字符过滤,Spring 的dispatcher等。

webx.xml代码如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns="http://Java.sun.com/xml/ns/javaee"    
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    
  5.                              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    
  6.          version="2.5" >  
  7.        
  8.     <!-- 区分项目名称,防止默认重名 -->  
  9.     <context-param>  
  10.         <param-name>webAppRootKey</param-name>  
  11.         <param-value>maven.example.root</param-value>  
  12.     </context-param>  
  13.   
  14.     <!-- Spring的log4j监听器 -->  
  15.     <listener>  
  16.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  17.     </listener>  
  18.   
  19.     <!-- 字符集 过滤器  -->  
  20.     <filter>  
  21.         <filter-name>CharacterEncodingFilter</filter-name>  
  22.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  23.         <init-param>  
  24.             <param-name>encoding</param-name>  
  25.             <param-value>UTF-8</param-value>  
  26.         </init-param>  
  27.         <init-param>  
  28.             <param-name>forceEncoding</param-name>  
  29.             <param-value>true</param-value>  
  30.         </init-param>  
  31.     </filter>  
  32.     <filter-mapping>  
  33.         <filter-name>CharacterEncodingFilter</filter-name>  
  34.         <url-pattern>/*</url-pattern>  
  35.     </filter-mapping>  
  36.   
  37.     <!-- Spring view分发器 -->  
  38.     <servlet>  
  39.         <servlet-name>dispatcher</servlet-name>  
  40.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  41.         <init-param>  
  42.             <param-name>contextConfigLocation</param-name>  
  43.             <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>  
  44.         </init-param>  
  45.         <load-on-startup>1</load-on-startup>  
  46.     </servlet>  
  47.     <servlet-mapping>  
  48.         <servlet-name>dispatcher</servlet-name>  
  49.         <url-pattern>*.do</url-pattern>  
  50.     </servlet-mapping>  
  51.   
  52. </web-app>  

4.2 编写Spring配置文件dispatcher-servlet.xml

如要添加MVC驱动、注解检测、视图解析等。dispatcher-servlet.xml代码如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.        xmlns:aop="http://www.springframework.org/schema/aop"    
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:mvc="http://www.springframework.org/schema/mvc"    
  6.        xmlns:tx="http://www.springframework.org/schema/tx"    
  7.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  8.        xsi:schemaLocation="http://www.springframework.org/schema/aop    
  9.         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd    
  10.         http://www.springframework.org/schema/beans    
  11.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  12.         http://www.springframework.org/schema/context    
  13.         http://www.springframework.org/schema/context/spring-context-3.0.xsd    
  14.         http://www.springframework.org/schema/mvc    
  15.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    
  16.         http://www.springframework.org/schema/tx    
  17.         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  
  18.   
  19.     <mvc:annotation-driven />  
  20.     <context:component-scan base-package="liming.maven.example" />  
  21.   
  22.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  23.         <property name="prefix" value="/WEB-INF/views/" />  
  24.         <property name="suffix" value=".jsp" />  
  25.     </bean>  
  26.   
  27. </beans>  

4.3 编写一个Controller层测试类

编写一个SpringMVC的Controller层测试类。此类只有一个方法做地址映射,并向页面传递一个数据。代码如下:

  1. package liming.maven.example.view;   
  2.   
  3. import org.springframework.stereotype.Controller;   
  4. import org.springframework.ui.Model;   
  5. import org.springframework.web.bind.annotation.RequestMapping;   
  6.   
  7. @Controller  
  8. public class GeneralController {   
  9.   
  10.     @RequestMapping(value="index.do")   
  11.     public void index_jsp(Model model){   
  12.         model.addAttribute("liming", "黎明你好");   
  13.         System.out.println("index.jsp");   
  14.     }   
  15. } 

4.4 编写index.jsp页面
首先在src/main/webapp/WEB-INF下建文件夹views。此处和dispatcher-servlet.xml配置文件中的prefix属性路径要一样。

在views下建index.jsp文件

我们使用jstl获取Controlleradd的数据。

Jsp页面代码如下:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5.     <head>  
  6.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7.         <title>Insert title here</title>  
  8.     </head>  
  9.        
  10.     <body>  
  11.         <c:out value="${liming}"></c:out>  
  12.     </body>  
  13. </html>  
linux
相关资讯       Eclipse教程  Maven  SpringMVC 
本文评论   查看全部评论 (2)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 2 楼
* 匿名 发表于 2013/11/23 10:38:27
不错
第 1 楼
* 匿名 发表于 2013/11/23 10:36:15
不错不错