zoukankan      html  css  js  c++  java
  • springMVC入门配置案例

    1、spring的jar包下载

      进入http://repo.springsource.org/libs-release-local/,然后依次点击org/-->springframework-->spring,即可根据需要下载spring框架的压缩包。

    2、common-logging的jar包

      spring核心容器依赖common-logging的jar包,下载地址http://commons.apache.org,依次点击Releases-->Logging,可选择commons-logging-1.2-bin.zip下载。

    3、springMVC的DispatcherServlet

      DispatcherServlet是一个Servlet,继承自HttpServlet。它是springMVC的前端控制器,用于拦截请求,与客户端交互。

    4、springMVC入门案例

      环境搭建:建立dynamic web project,导入springMVC需要的jar包,在web.xml中配置springMVC前端控制器,编写springMVC.xml核心配置文件,编写controller。

    web.xml中配置springMVC前端控制器

     1 <!-- 配置springMVC前段控制器 -->
     2   <servlet>
     3       <servlet-name>springMVC</servlet-name>
     4       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     5       <init-param>
     6           <param-name>contextConfigLocation</param-name>
     7           <param-value>/WEB-INF/springMVC.xml</param-value>
     8       </init-param>
     9       <!-- Tomcat启动时加载该servlet -->
    10       <load-on-startup>1</load-on-startup>
    11   </servlet>
    12   <servlet-mapping>
    13       <servlet-name>springMVC</servlet-name>
    14       <url-pattern>/</url-pattern>
    15   </servlet-mapping>

    核心配置文件springMVC.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4         xmlns:context="http://www.springframework.org/schema/context"
     5         xmlns:mvc="http://www.springframework.org/schema/mvc"
     6         xsi:schemaLocation="http://www.springframework.org/schema/beans
     7         http://www.springframework.org/schema/beans/spring-beans.xsd
     8         http://www.springframework.org/schema/context
     9         http://www.springframework.org/schema/context/spring-context.xsd
    10         http://www.springframework.org/schema/mvc
    11         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    12     
    13     <!-- 配置@Controller注解扫描 -->
    14     <context:component-scan base-package="com.alphajuns.controller"></context:component-scan>
    15     <!-- 配置处理器映射器 -->
    16     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
    17     <!-- 配置处理器适配器 -->
    18     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
    19     <!-- 配置视图解析器 -->
    20     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
    21 </beans>

    controller类

     1 package com.alphajuns.controller;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 import org.springframework.web.servlet.ModelAndView;
     6 
     7 @Controller
     8 public class HelloController {
     9 
    10     @RequestMapping(value="/hello")
    11     public ModelAndView hello() {
    12         // 创建模型和视图对象
    13         ModelAndView mv = new ModelAndView();
    14         // 添加模型
    15         mv.addObject("message", "Hello SpringMVC!");
    16         // 设置视图
    17         mv.setViewName("/WEB-INF/content/welcome.jsp");
    18         // 返回模型和视图
    19         return mv;
    20     }
    21 }

    页面的话主要是取值,可用el表达式,如welcome.jsp页面

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html>
     4 <html>
     5 <head>
     6 <meta charset="UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     ${requestScope.message }
    11 </body>
    12 </html>

    访问成功

    如果不指定加载配置文件路径,默认情况下,加载springMVC配置文件会去WEB-INF文件夹下查找对应的[servlet-name]-servlet.xml。

    解析springMVC配置文件会根据配置文件创建一个WebApplicationContext容器对象,WebApplicationContext继承自ApplicationContext容器。

    springMVC建议把所有视图页面存放在WEB-INF文件夹下,这样可以保护视图,避免直接向视图页面发送请求。

  • 相关阅读:
    DataAnnotations
    使用BizTalk实现RosettaNet B2B So Easy
    biztalk rosettanet 自定义 pip code
    Debatching(Splitting) XML Message in Orchestration using DefaultPipeline
    Modifying namespace in XML document programmatically
    IIS各个版本中你需要知道的那些事儿
    关于IHttpModule的相关知识总结
    开发设计的一些思想总结
    《ASP.NET SignalR系列》第五课 在MVC中使用SignalR
    《ASP.NET SignalR系列》第四课 SignalR自托管(不用IIS)
  • 原文地址:https://www.cnblogs.com/alphajuns/p/11020595.html
Copyright © 2011-2022 走看看