zoukankan      html  css  js  c++  java
  • springmvc基本配置

    1.开发环境搭建,我用的是eclipse+tomcat7+jdk1.6,这部分不做介绍

    2.新建工程,我新建的是dynamic web project 命名为springmvc1

    3.导入jar包,我用的是springframework3.1.1,目前官网spring不能下载,我是下载的源文件,用ant编译的。需要导入的jar包有:

    4.编写WEB-INF/web.xml如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
        <display-name></display-name>
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <filter>
            <filter-name>encodingFilter</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>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <!-- springMVC 配置 -->
        <servlet>
            <description>spring mvc servlet</description>
            <servlet-name>springMvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <description>spring mvc 配置文件</description>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <!-- 拦截以html为后缀的请求 -->
        <servlet-mapping>
            <servlet-name>springMvc</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>10</session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>/views/index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

    5.编写spring-mvc.xml以及spring.xml文件

    spring-mvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
        <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
        <context:annotation-config />
        <context:component-scan base-package="com.rainbow.controller" />
    
        <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
        <bean
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    
        </bean>
    
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views" />
            <property name="suffix" value=".jsp" />
        </bean>
    </beans>

    spring.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"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        ">
    
        <!-- 引入属性文件 -->
        <context:property-placeholder location="classpath:database.properties" />
    
        <!-- 自动扫描controller,dao,service包(自动注入) -->
        <context:annotation-config />
        <context:component-scan base-package="com.rainbow" />
        
    
    </beans>

    6.编写controller文件,在src目录下建立package

    package com.rainbow.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class TestController {
    
        @RequestMapping("/hello")
        public String hello(){
            return "/hello";
        }
    }

    以上是简单的java文件,

    7.编写视图文件,hello.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">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    Hello world!
    </body>
    </html>

    8.run 项目, 我们输入http://localhost:9090/springmvc1/hello.do便可以获得hello world的页面输出,以上是spring最基本的配置。 目前并没有显示数据层的东西。

    主要是给自己做一下记录,防止忘记。

    下一篇介绍,导入数据库,以及hibernate。


    这部分准备放spring原理相关的东西

  • 相关阅读:
    高效真实的云渲染算法 【转】
    网络字节顺序 【转】
    Windows网络编程 2 【转】
    windows 网络编程[转]
    电信诈骗
    winsock 收发广播包 【转】
    windows核心编程 DLL技术 【转】
    CityEngine结合ArcGIS制作地质体web场景 [转]
    共有49款Windows GUI开发框架开源软件 【转】
    详解分级基金(杠杆基金)【精华】 【转】
  • 原文地址:https://www.cnblogs.com/snail-tomorrow/p/3482025.html
Copyright © 2011-2022 走看看