zoukankan      html  css  js  c++  java
  • 初试 spring web mvc

    作为一名code需要了解更多的知识,编程方面的东西太多了,是个逐渐积累的过程。最近学习一下spring web mvc,写下我个人的一些经验。

    1。准备jar包。spring mvc 已经到了版本4,网上的很多资料已经不在适用。给出的下载地址也无法适用。这是非常痛苦的。我费了好大功夫才获得这些JAR包。

    在官方介绍文档里获得http://docs.spring.io/spring/docs/4.1.0.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#overview-distribution-zip

    在这里下载:http://repo.spring.io/release/org/springframework/spring/4.0.1.RELEASE/

    同时还需要jstl包,你可以搜索到。

    2. 创建一个web project项目,将jar包放入工程的WEB-INF的lib下。如下图:

    3,创建 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" version="2.5">
      <display-name>Web Application</display-name>
      <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>
    				/WEB-INF/conf/spring/spring-servlet.xml
    			</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>*.htm</url-pattern>
      </servlet-mapping>
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
    			/WEB-INF/conf/spring/spring-servlet.xml
    		</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    </web-app>
    

      

    4.创建文件夹WEB-INF/conf/spring,再创建文件spring-servlet.xml 内容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- 配置SpringMVC配置文件所需的xml文件解析模板 -->
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core"
        xmlns:tx="http://www.springframework.org/schema/tx" 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://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd 
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
        <!-- 配置控制器要掃描的包路徑 -->
        <context:component-scan base-package="zyf.demo" />
        <!-- 配置視圖解析器 -->
        <bean id="jspViewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"
                value="org.springframework.web.servlet.view.JstlView" />
            <!-- 解析器解析/WEB-INF/jsp/路徑下,以.jsp結尾的視圖文件 -->
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    </beans>

    5.写控制器

    package zyf.demo;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("/demo")
    public class HelloWorldController {
       
        @RequestMapping("/helloWorld")//url字段名
        public String helloWorld() {
            return "helloWorld";//jsp文件名
        }
    }

    6.写 index.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%
        request.getRequestDispatcher("/demo/helloWorld.htm").forward(request, response);
    %>

    大功告成。中间更多的时间浪费在找jar包问题上。

  • 相关阅读:
    GitLab 远程 定时备份
    GitLab 本地 定时备份
    MATLAB格式化输出控制
    hilbert矩阵
    MATLAB符号运算
    双线性插值 分类: 图像处理 2015-07-28 15:14 7人阅读 评论(0) 收藏
    shamir叠像术 分类: 图像处理 2015-07-08 16:50 17人阅读 评论(1) 收藏
    cookies、sessionStorage和localStorage解释及区别
    微信小程序,组件之间带参数跳转+轮播图+冒泡事件+表单提交
    微信小程序,头部和底部设置需要注意的事项
  • 原文地址:https://www.cnblogs.com/vir56k/p/3757145.html
Copyright © 2011-2022 走看看