http://7xvpsh.com1.z0.glb.clouddn.com/publish/21-2/the-dispatcher-servlet.html

springmvc4.1.7:配置

复制转载大神笔记

https://blog.csdn.net/lpch0825/article/details/79391982

1、导入jar包

注解主要在spring-webmvc-3.2.8.RELEASE.jar中

2、web.xml配置文件

web.xml中主要配置springMVC的前端控制器(核心控制器)

注:这里采用xxx-servlet.xml默认命名方式,并且文件位于/WEB-INF目录下,所以在web.xml中不需要配置<init-param></init-param>

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  3.  
    <display-name>spring_mvc_annotation</display-name>
  4.  
     
  5.  
    <!-- springMVC的前端控制器 -->
  6.  
    <servlet>
  7.  
    <servlet-name>springmvc</servlet-name>
  8.  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  9.  
    </servlet>
  10.  
    <servlet-mapping>
  11.  
    <servlet-name>springmvc</servlet-name>
  12.  
    <url-pattern>/</url-pattern>
  13.  
    </servlet-mapping>
  14.  
     
  15.  
    </web-app>

3、springmvc-servlet.xml配置文件

注意:表头这里添加mvc声明,声明的地址在 spring-webmvc-3.2.8.RELEASE.jar 中的  META-INF/spring.schemas 中。这样以后<mvc:xxx>标签才会有效

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  3.  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
        xmlns:mvc="http://www.springframework.org/schema/mvc" <!-- 注意添加mvc声明 -->
  5.  
        xmlns:aop="http://www.springframework.org/schema/aop"
  6.  
        xmlns:tx="http://www.springframework.org/schema/tx"
  7.  
        xmlns:context="http://www.springframework.org/schema/context"
  8.  
        xsi:schemaLocation="
  9.  
       http://www.springframework.org/schema/beans
  10.  
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  11.  
       http://www.springframework.org/schema/aop
  12.  
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  13.  
       http://www.springframework.org/schema/tx
  14.  
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  15.  
       http://www.springframework.org/schema/mvc <!-- 注意添加mvc声明 -->
  16.  
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd <!-- 注意添加mvc声明,注意版本,不写版本会有默认版本 -->
  17.  
       http://www.springframework.org/schema/context     
  18.  
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  19.  
        
  20.  
        <mvc:annotation-driven/>
  21.  
        <context:component-scan base-package="com.hfxt.controller"></context:component-scan>    
  22.  
         <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图  根据控制器返回的字符串拼接成jsp路径:/WEB-INF/page/xx.jsp -->
  23.  
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  24.  
            <property name="prefix" value="/WEB-INF/page/"/><!-- 前缀 -->
  25.  
            <property name="suffix" value=".jsp"/><!-- 后缀 -->
  26.  
        </bean>
  27.  
    </beans>

4、控制层(controller层)

注意:这里的login.jsp已经放入/WEB-INF/page目录下,为了方便视图解析器处理:是进入index.jsp页面还是返回login.jsp登录页面。这时初次进入login.jsp就需要利用toLogin()方法了。

  1.  
    package com.hfxt.controller;
  2.  
     
  3.  
    import javax.servlet.http.HttpSession;
  4.  
    import org.springframework.stereotype.Controller;
  5.  
    import org.springframework.ui.Model;
  6.  
    import org.springframework.web.bind.annotation.RequestMapping;
  7.  
    import org.springframework.web.bind.annotation.RequestMethod;
  8.  
    @Controller        //在类上面定义,表明该类为控制器,返回字符串与redirect:xxx
  9.  
    @RequestMapping(value="/") //在类或方法上面使用此注解,设置URL访问地址。它有两个属性,value指定访问路径,method指定指定请求方式,请求方式在RequestMethod这个类中,全部以常量形式定义,它默认使用GET请求。
  10.  
    public class LoginController {
  11.  
    @RequestMapping(value="/login",method=RequestMethod.GET)    //访问.../login,方式为get时,该方法处理请求
  12.  
    public String toLogin(){
  13.  
    return "login";
  14.  
    }
  15.  
     
  16.  
    @RequestMapping(value="/login",method=RequestMethod.POST)
  17.  
    public String doLogin(String username , String password , Model model , HttpSession session){    //访问.../login,方式为post时,该方法处理请求
  18.  
    if("admin".equals(username)&&"123".equals(password)){
  19.  
    session.setAttribute("username", username);
  20.  
    model.addAttribute("message", "登录成功!");
  21.  
    return "index";
  22.  
    }else{
  23.  
    model.addAttribute("message", "登录失败!");
  24.  
    return "login";
  25.  
    }
  26.  
    }
  27.  
    }

5、jsp页面

login.jsp页面代码

  1.  
    <?xml version="1.0" encoding="UTF-8" ?>
  2.  
    <%@ page language="java" contentType="text/html; charset=UTF-8"
  3.  
    pageEncoding="UTF-8"%>
  4.  
    <!DOCTYPE html >
  5.  
    <html xmlns="http://www.w3.org/1999/xhtml">
  6.  
    <head>
  7.  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  8.  
    <title>Insert title here</title>
  9.  
    </head>
  10.  
    <body>
  11.  
    <form action="/spring_mvc_annotation/login" method="post">
  12.  
    用户名:<input type="text" name="username" value=""/><br />
  13.  
    密码:<input type="password" name="password" value=""/><br />
  14.  
    <input type="submit" value="登录"/>
  15.  
    </form>
  16.  
    <div style="color: red">${message }</div>
  17.  
    </body>
  18.  
    </html>

index.jsp页面代码

  1.  
    <?xml version="1.0" encoding="UTF-8" ?>
  2.  
    <%@ page language="java" contentType="text/html; charset=UTF-8"
  3.  
    pageEncoding="UTF-8"%>
  4.  
    <!DOCTYPE html>
  5.  
    <html xmlns="http://www.w3.org/1999/xhtml">
  6.  
    <head>
  7.  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  8.  
    <title>Insert title here</title>
  9.  
    </head>
  10.  
    <body>
  11.  
    <h1>${sessionScope.username },${message }</h1>
  12.  
    </body>
  13.  
    </html>
 

Spring MVC是当前最优秀的MVC框架,自从Spring 2.5版本发布后,由于支持注解配置,易用性有了大幅度的提高。上一篇博文已经介绍了最简单的配置文件的详情,这里再介绍一下最简单的注解配置详情,毕竟springMVC是鼓励使用注解的。

1、导入jar包

注解主要在spring-webmvc-3.2.8.RELEASE.jar中

2、web.xml配置文件

web.xml中主要配置springMVC的前端控制器(核心控制器)

注:这里采用xxx-servlet.xml默认命名方式,并且文件位于/WEB-INF目录下,所以在web.xml中不需要配置<init-param></init-param>

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  3.  
    <display-name>spring_mvc_annotation</display-name>
  4.  
     
  5.  
    <!-- springMVC的前端控制器 -->
  6.  
    <servlet>
  7.  
    <servlet-name>springmvc</servlet-name>
  8.  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  9.  
    </servlet>
  10.  
    <servlet-mapping>
  11.  
    <servlet-name>springmvc</servlet-name>
  12.  
    <url-pattern>/</url-pattern>
  13.  
    </servlet-mapping>
  14.  
     
  15.  

springMvc配置 中文api的更多相关文章

  1. [转载]fullPage.js中文api 配置参数~

    fullPage.js中文api 配置参数 选项 类型 默认值 说明 verticalCentered 字符串 true 内容是否垂直居中 resize 布尔值 false 字体是否随着窗口缩放而缩放 ...

  2. [置顶] COcos2d-X 中文API

    本文来自http://blog.csdn.net/runaying ,引用必须注明出处! COcos2d-X 中文API 温馨提醒:使用二维码扫描软件,就可以在手机上访问我的博客啦!另外大家可以访问另 ...

  3. SpringMVC配置实例

    一.SpringMVC概述 MVCII模式实现的框架技术 Model--业务模型(Biz,Dao...) View--jsp及相关的jquery框架技术(easyui) Contraller--Dis ...

  4. Spring-MVC配置Gson做为Message Converter解析Json

    Spring-MVC配置Gson做为Message Converter解析Json 在学习Spring的时候看到可以使用@RequestBody 和@ResponseBody注解来是的Spring自动 ...

  5. redis中文API

    1.学习文档地址:http://www.redisdoc.com/en/latest/index.html 2.redis中文API REDIS所有的命令 <<ABOUT LIST> ...

  6. ElasticSearch搜索引擎安装配置中文分词器IK插件

    近几篇ElasticSearch系列: 1.阿里云服务器Linux系统安装配置ElasticSearch搜索引擎 2.Linux系统中ElasticSearch搜索引擎安装配置Head插件 3.Ela ...

  7. FreeMarker学习(springmvc配置)

    springMvc配置 <bean id="freemarkerConfig" class="org.springframework.web.servlet.vie ...

  8. springmvc字符 中文乱码问题

    springmvc字符 中文乱码问题 1.字符过滤器 输入中文测试,发现乱码 以前乱码问题通过过滤器解决 , 而SpringMVC给我们提供了一个过滤器 , 可以在web.xml中配置,修改了xml文 ...

  9. Slackware Linux or FreeBSD 配置中文环境。

    配置中文环境. Slackware Linux 如果在控制面板的语言与地区选项中没有找到中文,那说明在安装系统选择软件的时候没有将国际语言支持包选上,可以从slackware的安装盘或ISO文件中提取 ...

随机推荐

  1. 洛谷【P1898】缘分计算

    我对模拟的理解:http://www.cnblogs.com/AKMer/p/9064018.html 题目传送门:https://www.luogu.org/problemnew/show/P189 ...

  2. 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较

        几大最短路径算法比较 转自:http://blog.csdn.net/v_july_v/article/details/6181485 几个最短路径算法的比较: Floyd        求多 ...

  3. Ubuntu18.04安装Docker, centos7安装Docker

    Ubuntu18.04安装Docker 第一种方法从Ubuntu的仓库直接下载安装: 安装比较简单,这种安装的Docker不是最新版本,不过对于学习够用了,依次执行下面命令进行安装. $ sudo a ...

  4. MyEclipse、Eclipse SVN插件的帐号、密码修改

    问题描述: Eclipse的SVN插件Subclipse做得很好,在svn操作方面提供了很强大丰富的功能.但到目前为止,该插件对svn用户的概念极为淡薄,不但不能方便地切换用户,而且一旦用户的帐号.密 ...

  5. Python-requests取消SSL验证的警告InsecureRequestWarning解决办法

    使用requests模块请求一个证书无效的网站的话会直接报错 可以设置verify参数为False解决这个问题 # -*- coding:utf-8 -*- __author__ = "Mu ...

  6. JS取得绝对路径

    在项目中,我们经常要得到项目的绝对路径,方便我们上传下载文件,JS为我们提供了方法,虽说要迂回一下.代码如下: function getRealPath(){        //获取当前网址,如: h ...

  7. centos6.5安装zookeeper教程(三)

    阅读前建议先阅读: http://www.cnblogs.com/duenboa/articles/6665159.html   1. 下载安装文件zookeeper-3.4.6.tar.gz 镜像地 ...

  8. 【问题】Expandable数据集的定义的正确方法,TabActivity弃用替代,Gallery替代,imageswitcher

    Expandable 问题: http://www.cnblogs.com/xingyyy/p/3389611.html 扩展阅读:http://blog.csdn.net/lmj623565791/ ...

  9. assert.ok()

    测试 value 是否为真值. 相当于 assert.equal(!!value, true, message). 如果 value 不为真值,则抛出一个带有 message 属性的 Assertio ...

  10. Ubuntu 使用 heirloom-mail 调用外部邮箱 SMTP 服务器发送邮件

    使用本地服务发邮件,经常被过滤掉而且占用资源,发送成功率不高.所以使用外部SMTP服务器发送邮件成为了需求. SMTP认证的目的是为了使用户避免受到垃圾邮件的侵扰,简单地说就是要求必须在提供了账户名和 ...