美文网首页
spring mvc mini(一)

spring mvc mini(一)

作者: 北海北_6dc3 | 来源:发表于2019-10-13 20:53 被阅读0次

创建maven的项目

1、使用idea创建一个空的maven项目---并引入相关依赖
   <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>jc.learn</groupId>
    <artifactId>spring mvc mini</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <jdk.version>1.8</jdk.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.25.RELEASE</version>
        </dependency>
    </dependencies>

</project>
2、创建一个maven项目---web.xml【路径/src/main/webapp/WEB-INF/web.xml】

根据tomcat和jdk创建web.xml版本
web.xml头信息汇总
此处,我们使用servlet 3.1
写入配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
                <!-- spring 入口-->
            <param-value>classpath:config/spring-core.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- 中文乱码处理 -->
   <filter>  
        <filter-name>characterEncodingFilter</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>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>characterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping> 
</web-app>
3、使用idea创建一个spring-core.xml【路径resources/config/spring-core.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 使用注解方式完成映射 -->
    <!-- 让扫描spring扫描这个包下所有的类,让标注spring注解的类生效 -->
    <context:component-scan base-package="com.jc.controller"/>
    <mvc:annotation-driven/><!-- 开启注解 -->

    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>
4、使用idea创建一个controller【路径java/com/jc/controller/HelloController】,包路径:com.jc.controller
package com.jc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author ivan
 * @version V1.0
 * @Description: TODO
 * @date 2019/10/13 18:58
 */
@Controller
public class HelloController {
    @RequestMapping(value="/welcome1")  //welcome要访问的url地址
    public  String  hello1(String uname,Model model){//此时方法参数与传来参数名称一致
        System.out.println("hello,springmvc"+uname);
        model.addAttribute("username","张三1112");
        return "hello";  //hello是逻辑视图名,和后缀名组合一起构成视图名  /web-inf/jsp/hello.jsp
    }
}
5、创建视图 hello.jsp【路径webapp/web-inf/jsp/hello.jsp】
<html>
<body>
<h2>Hello World!${username}</h2>
</body>
</html>
6、创建tomcat,选择包。并设置/为根路径
image.png
7、配置tomcat相关信息
image.png
通过路径http://localhost:8080/welcome1展示访问
image.png

git地址:
https://code.aliyun.com/liyi1314/quicklyCreateSpringMvc.git

下一章,我们将引入spring cache基于redis和ehcache的切换

参考资料

4.3.25.RELEASE英文参考手册
4.3.21.RELEASE中文参考手册

mvc简单配置参考

spring 模块

image.png

我们看下,生成的maven依赖:

spring依赖.png
我们明明只引入了spring-webmvc,怎么生成了那么多?点击进入spring-webmvc maven依赖。
image.png
即我们可以看到spring-webmvc中包含spring-web,spring-core等

相关文章

网友评论

      本文标题:spring mvc mini(一)

      本文链接:https://www.haomeiwen.com/subject/ydwcmctx.html