一、利用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[]{"/"};
    }
}

springmvc框架简单搭建的更多相关文章

  1. Asp.net框架与SpringMvc框架简单分析

    (此文为自我总结,错误很多请勿借鉴) 1.就前两天分析SpringMvc框架中是通过controler来实现跳转页面,通过mapping来实现数据连接 分析的方法又通过java的类之间进行相互调用,个 ...

  2. ssh框架简单搭建

    这里是个人对SSH框架搭建的一点心得,仅供新手,勿喷 首先,搞清楚分层, 视图层 --> 控制层  --> 业务层 --> DAO层--> 持久层 搭建的顺序是从后向前,搭建一 ...

  3. PHP之MVC微型框架简单搭建

    好长时间没有写博客了,这段时间过得紧张的不行,以致于都没有抽出时间来好好的总结一下自己这段时间的生活和学习. 其实今天不想写什么技术博客,就想简单总结一下这段时间的生活.10月8号从家里回来以后决定自 ...

  4. SpringMVC 框架的搭建及基本功能的实现

    首先新建一个WEB项目 导入jar包 我们基于Spring mvc框架进行开发,需要依赖一下的spring jar包: spring-aop-4.0.4.RELEASE.jar spring-bean ...

  5. Spring学习之SpringMVC框架快速搭建实现用户登录功能

    引用自:http://blog.csdn.net/qqhjqs/article/details/41683099?utm_source=tuicool&utm_medium=referral  ...

  6. Django学习(二) Django框架简单搭建

    为了快速学习Python进行Web的开发,所以我不准备从Python的基础学起,直接从Django框架入手,边学框架边学Python的基础知识. 下面就开始Django的快速开发之旅吧. 关于Djan ...

  7. springmvc框架的搭建

    1引入jar包 jar包下载地址http://maven.springframework.org/release/org/ 以下是我引入的jar包 aopalliance-1.0.jaraspectj ...

  8. 新手springmvc web简单搭建过程-caidachun

    第一次做整了一天,中间各种报错,大佬肯定要嘲笑我了,但还是很有成就敢,现在只知道怎么装配,还不知道为什么,还需要学习.加油 1.新建动态web项目 2. 添加jar包,可以到网上下载,commons- ...

  9. Idea搭建SpringMVC框架(初次接触)

    公司转Java开发,做的第一个项目是SpringMVC框架,因为底层是同事封装,等完成整个项目,对SpringMVC框架的搭建还不是很了解,所以抽时间不忙的时候自己搭建了一个SpringMVC框架. ...

随机推荐

  1. java web 程序---登陆验证session。提示登陆

    loigin.jsp <%@ page language="java" import="java.util.*" pageEncoding="g ...

  2. Java 方法签名

    方法签名格式: 方法名   参数列表 例如: public class A{ protected int method (int a, int b) { return 0; } } class B e ...

  3. python 将html实体转回去

    参考资料: http://www.360doc.com/content/17/0620/16/44530822_664927373.shtml https://blog.csdn.net/guzhou ...

  4. apk比较版本大小

    Java String.compareTo(), 此方法如果这个字符串是等参数字符串那么​返回值0,如果这个字符串是按字典顺序小于字符串参数那么返回小于0的值, 如果此字符串是按字典顺序大于字符串参数 ...

  5. Cisco动态路由 OSPF协议

    OSPF描述: 组播扩展OSPF  锁定 同义词 ospf一般指组播扩展OSPF 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . OSPF(Open Shortest Path Firs ...

  6. 转:Numpy教程

    因为用到theano写函数的时候饱受数据结构困扰 于是上网找了一篇numpy教程(theano的数据类型是基于numpy的) 原文排版更好,阅读体验更佳: http://phddreamer.blog ...

  7. teamviewer14商用试用期到期从新安装使用

    teamviewer14商用试用期到期从新安装使用 1)1.退出TeamViewer远程软件,卸载软件.2)2.按键盘的[win]+[R]组合键打开[运行],输入 %appdata%3)3.在弹出的窗 ...

  8. leetcode703

    class KthLargest { public: KthLargest(int k, vector<int> nums) { size = k; for(auto num:nums){ ...

  9. VB导入Excel到数据库软件(持续更新中。)

    1.选择Excel文件版本 电脑上用的 Office2010 引用:Mircosoft Excel 14.0 Object Library 2.选择Excel文件 '选择文件公共变量 Public D ...

  10. python打开浏览器的三种方法

    1.startfile方法 import os os.startfile("C:\Program Files (x86)\Google\Chrome\Application\chrome.e ...