• [架构]myeclipse配置SpringMVC 以及简单应用 教程


    1.Spring的安装

      

      

       

            

      等待安装完成。其中myeclipse会自动导入所需的jar包

    2.SpringMVC的配置

      在WEB-INF目录下创建web.xml文件

      

      web.xml内容如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app version="3.0" 
     3     xmlns="http://java.sun.com/xml/ns/javaee" 
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     6     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
     7     <!-- 配置SpringMVC -->
     8     <servlet>
     9         <servlet-name>dispatcher</servlet-name>
    10         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    11         <init-param>  
    12             <param-name>contextConfigLocation</param-name>  
    13             <param-value>classpath:dispatcher-servlet.xml</param-value>  
    14         </init-param> 
    15     </servlet>
    16 
    17     <servlet-mapping>
    18         <servlet-name>dispatcher</servlet-name>
    19         <!-- 监听所有请求 -->
    20         <url-pattern>hello</url-pattern>
    21     </servlet-mapping>
    22 </web-app>

      注:其中

    <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <!-- 监听所有请求 -->
            <url-pattern>hello</url-pattern>
    </servlet-mapping>

     <url-pattern>/</url-pattern> 相当于一个小区的门卫大爷,会拦截所有hello的请求

    继而门卫大爷会直接给请求进入的人根据 <servlet-name>dispatcher</servlet-name> 中的dispatcher找 <servlet> 中的 <servlet-name>dispatcher</servlet-name> 

    找到如下部分代码:

        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>  
                <param-name>contextConfigLocation</param-name>  
                <param-value>classpath:dispatcher-servlet.xml</param-value>  
            </init-param> 
        </servlet>

    dispatcher中指明 <param-value>classpath:dispatcher-servlet.xml</param-value>  所以接下来创建dispatcher-servlet.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:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">  
           <!-- 定义要扫描 controller的包 -->
           <context:component-scan base-package="com.frank.springmvc.controller" />
    
           <mvc:default-servlet-handler /> 
           <!-- 启动注解驱动 SpringMVC 功能 -->
           <mvc:annotation-driven />
    
           <!-- 配置视图解析器解析路径 -->
           <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
                  <!-- 定义视图存放路径 -->
                  <property name="prefix" value="/WEB-INF/" />
                  <!-- 定义视图后缀 -->
                  <property name="suffix" value=".jsp" />
           </bean>
    </beans>

     <context:component-scan base-package="com.frank.springmvc.controller" /> 是你要创建的controller的包名,可自行更改但必须保持一致。

    <!-- 配置视图解析器解析路径 -->中的
     <property name="prefix" value="/WEB-INF/" /> 为前缀

     <property name="suffix" value=".jsp" /> 为后缀

    在下面会详细解释

    3.SpringMVC的简单应用

    接下来创建package

      

      

    包名如上

     com.frank.springmvc.controller 

       

     在该包中创建HellpSpringMVC类代码如下:

     1 package com.frank.springmvc.controller;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 
     6 @Controller
     7 public class HellpSpringMVC {
     8 
     9     @RequestMapping("hello")
    10     public String hello() {
    11         return "hello";
    12     }
    13 }

     @RequestMapping("hello") 这就是请求的hello,

    在WEB-INF中创建hello.jsp

    代码如下:

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>My JSP 'hello.jsp' starting page</title>
    13     
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22 
    23   </head>
    24   
    25   <body>
    26     Spring Hello - Welcome<br>
    27   </body>
    28 </html>
    View Code

    更改index.jsp的代码:

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>My JSP 'index.jsp' starting page</title>
    13     <meta http-equiv="pragma" content="no-cache">
    14     <meta http-equiv="cache-control" content="no-cache">
    15     <meta http-equiv="expires" content="0">    
    16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    17     <meta http-equiv="description" content="This is my page">
    18     <!--
    19     <link rel="stylesheet" type="text/css" href="styles.css">
    20     -->
    21   </head>
    22   
    23   <body>
    24     <a href="hello">Hello</a>
    25   </body>
    26 </html>
    View Code

    其中 <a href="hello">Spring Hello</a> 请求为hello会被web.xml的setvlet-mapping拦截

    根据Controller类 HellpSpringMVC 的 @RequestMapping("hello") 执行 public String hello()  返回hello字符串

    根据dispatcher-servlet.xml视图解析器(详见:上面的前缀以及后缀

    拼接成 /WEB-INF/   hello   .jsp    这就是返回路径

    结果如下:

      

       点击hello进入

      

  • 相关阅读:
    git设置用户名和邮箱
    D②商品列表:毫秒转换成时间;分页添加背景;$$搜索商品关键字 $$$清空
    C②参数管理:修改删除;空格分隔的字符串渲染成标签;动态编辑标签;删除标签
    有用的抓包工具Ethereal
    PHP与XML联手进行网站编程
    配置Apache 2.2+PHP 5.2.9支持OCI通过Oracle9i Client连接Oracle
    UTF8编码主页调用JS显示乱码问题解决办法
    WIndows 环境下安装php环境(php5.2.9+apache2.2安装过程)
    PHP5 在处理 XML 方面的改进
    marquee上下滚动停顿效果
  • 原文地址:https://www.cnblogs.com/zlc364624/p/12584735.html
Copyright © 2020-2023  润新知