• springmvc框架简单搭建


    一、利用xml 配置
    1.web.xml
     
    <web-app version="2.4"
        xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
     
        <display-name>Spring MVC Application</display-name>
        <servlet>
            <servlet-name>mymvc-dispatcher</servlet-name>
            <!--声明mvc控制器DispatcherServlet-->
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
           <!--声明mvc配置文件-->
           <!--如果不配置该选项,则默认扫描 ‘servlet-name’-servlet.xml文件 ,
           以该web.xml为例,如不配置,默认访问 mymvc-dispatcher-servlet.xml-->
            <init-param>
               <param-name>contextConfigLocation</param-name>
               <param-value>WEB-INF/mymvc.xml</param-value>
           </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
     
        <servlet-mapping>
            <servlet-name>mymvc-dispatcher</servlet-name>
            <!--请求路径-->
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
     
    2.mymvc.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd" >
    <mvc:annotation-driven />
    <context:component-scan base-package="com"/>
    <import resource="myjpa.xml"/>
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/page/"/>
    <property name="suffix" value=".jsp"/>
    </bean>

    </beans>

    二、利用java 配置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    package com.springapp.mvc;
     
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.ViewResolver;
    import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    import org.springframework.web.servlet.view.JstlView;
    import org.springframework.web.servlet.view.UrlBasedViewResolver;
     
    /**
     * Created by zzq on 2017/11/30.
     */
    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = "com")
    public class MvcConfig{
        @Bean
        public InternalResourceViewResolver setupViewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/pages/");
            resolver.setSuffix(".jsp");
    //        resolver.setViewClass(JstlView.class);
            return resolver;
        }
    }

           
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package com.springapp.mvc;
     
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.FilterType;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
     
    /**
     * Created by zzq on 2017/11/30.
     */
    @Configuration
    @ComponentScan(basePackages = "com",excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = EnableWebMvc.class)})
    public class RootConfig {
    }

    package com.springapp.mvc;
     
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
     
    /**
     * Created by zzq on 2017/11/30.
     */
    public class SpringMvcWebIn extends AbstractAnnotationConfigDispatcherServletInitializer {
        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class<?>[]{RootConfig.class};
        }
     
        @Override
        protected Class<?>[] getServletConfigClasses() {
            return new Class<?>[]{MvcConfig.class};
        }
     
        @Override
        protected String[] getServletMappings() {
            return new String[]{"/"};
        }
    }


  • 相关阅读:
    【转】【Egit】如何将eclipse中的项目上传至Git
    IntelliJ IDEA License Server本地搭建教程
    fastdfs-client-java工具类封装
    maven阿里云中央仓库
    Maven入门指南 :Maven 快速入门及简单使用
    如何在MyEclipse中配置jre的编译运行环境
    聚合函数 多次出现的某字段相同的记录。
    sql 过了试用期不能启动的,修改时间启动后还原。
    查看触发器内容
    Sql语句,先查询再插入一条语句完成。
  • 原文地址:https://www.cnblogs.com/anxbb/p/8433436.html
Copyright © 2020-2023  润新知