码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC-SimpleDEMO

时间:2017-10-16 01:54:20      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:处理   iso   实现   html4   中文乱码   属性   文件   请求   aging   

  本博文主要将如何配置一个简单的SpringMVC的DEMO,由上一讲的SpringMVC工作流程来看,配置一个SpringMVC的步骤是简单而清晰的。

  一、引入SpringMVC所需依赖       

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.jyk.springMVC</groupId>
    <artifactId>SpringMVC</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- <dependency> <groupId>org.springframework</groupId> <artifactId>spring-asm</artifactId> 
            <version>3.1.4.RELEASE</version> </dependency> -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>
</project>

  二、注册SpringMVC的核心控制器Dispather Servlet    

  在web.xml中注册Dispather Servlet

<?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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 通知DispacherServlet去指定的目录下加载springmvc.xml -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
            classpath:springmvc.xml
            </param-value>
        </init-param>
    </servlet>
    <!-- 核心过滤器过滤以.action结束的url请求 -->
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

    <!-- 注册spring核心编码过滤器,防止页面中文乱码 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app> 

  三、编写实现了Controller接口的处理器(即业务Action)

  实现springmvc的业务Action,返回ModelAndView对象,必须实现springmvc提供的Controller接口,路径为org.springframework.web.servlet.mvc.Controller

package com.jyk.springmvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloHAE implements Controller{

    private final Logger LOGGER = Logger.getLogger(HelloHAE.class);
    
    public HelloHAE() {
        LOGGER.info("HelloHAE hae been created");
    }
    
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        LOGGER.info("ModelAndView Entrance...");    
        /**
         * modelAndView对象封装了业务数据和前端显示的视图名称,即该业务数据最终会通过解析在代码设置的视图中显示
         * addObject方法为业务model添加属性
         * setViewName方法设置视图名称,此处配置的为逻辑视图名称success,配置成逻辑视图名称时,需要配置第四步所需要的视图解析器,若不想配置视图解析器,则此处可配置成
     * 真实视图名称/success.jsp
*/ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("Message","My First SpringMVC Application"); modelAndView.setViewName("success"); return modelAndView; } }

  四、配置请求与处理器映射关系

  由于第二步在web.xml配置了核心控制器Dispacher Servlet去加载的springmvc.xml,所以此处先配置全局的springmvc配置,再细节到具体的配置请求和处理器的映射关系的文件springmvc-001.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"
    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        
      ">

    <import resource="com/jyk/springmvc/springmvc-001.xml" />

</beans>      
<?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"        
      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       
      "> 
      
      <!-- 1、注册业务Action -->
      <bean name="/hello.action" class="com.jyk.springmvc.HelloHAE"></bean>
      
      <!-- 2、如果业务Action中配置的是逻辑视图名称,那么必须配置视图解析器,如第三步业务Action配置的是逻辑视图success,那么为了最终将数据送至success.jsp展示,那么必须
      配置如下的视图解析器,告知springmvc去哪里寻找最终要展示的目标视图,即/success.jsp
--> <!-- 3、如果业务Action中配置的是真实视图名称,即/success.jsp,那么视图解析器可选 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 路径前缀 --> <property name="prefix" value="/"></property> <!-- 路径后缀 --> <property name="suffix" value=".jsp"></property> <!-- 前缀+视图逻辑名+后缀 --> </bean> </beans>

   五、配置前端展示的视图

<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
   <!-- Message对应业务Action中addObject方法中的属性名 --> ${requestScope.Message}<br/> </body> </html>

  六、效果展示

技术分享

 

SpringMVC-SimpleDEMO

标签:处理   iso   实现   html4   中文乱码   属性   文件   请求   aging   

原文地址:http://www.cnblogs.com/jiyukai/p/7668873.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!