ssm: mybatis + spring + springmvc mvc三层架构
创建一个 maven 项目
导入 依赖包 在pom.xml
主要有 spring 框架核心库, spring mvc , servlet, jstl 等
<!--导入依赖-->
<dependencies>
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>4.12</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-webmvc</artifactid>
<version>5.1.9.release</version>
</dependency>
<dependency>
<groupid>javax.servlet</groupid>
<artifactid>servlet-api</artifactid>
<version>2.5</version>
</dependency>
<dependency>
<groupid>javax.servlet.jsp</groupid>
<artifactid>jsp-api</artifactid>
<version>2.2</version>
</dependency>
<dependency>
<groupid>javax.servlet</groupid>
<artifactid>jstl</artifactid>
<version>1.2</version>
</dependency>
</dependencies>
<!--由于maven可能存在资源过滤的问题,我们将配置完善-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
配置 web.xml
注意点
注意web.xml版本问题, 要最新版! version=4.0
注册 dispatcherservlet
关联 springmvc 的配置文件
启动级别为 1
映射路径为 / [不要用/*,会404]
<?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_4_0.xsd"
version="4.0">
<!--配置dispatchservlet:这个是springmvc 的核心: 也叫请求分发器 官方:叫前端控制器-->
<servlet>
<servlet-name>spring-02-annotation</servlet-name>
<servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
<!--dispatchservlet 要绑定 spring 的配置文件-->
<init-param>
<param-name>contextconfiglocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--启动级别:1-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--
在springmvc中 / 和 /* 的区别
/: 会只匹配所有的请求 不会去匹配jsp页面
/*: 匹配所有的请求, 包括jsp页面
-->
<!--所有请求都会被springmvc拦截-->
<servlet-mapping>
<servlet-name>spring-02-annotation</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
配置 springmvc-servlet.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<!-- 自动扫描包,让指定包下的注解生效,由ioc容器统一管理-->
<context:component-scan base-package="cn.itcast.controller"/>
<!-- 让 springmvc 不处理静态资源 默认过滤 .css .js .html .mp3 .mp4类似的文件-->
<mvc:default-servlet-handler/>
<!--
支持mvc注解驱动
在 spring 中一般采用@requestmapping注解来完成映射关系
要想使@requestmapping注解生效
必须向上下文注册defaultannotationhandlermapping
和一个 annotationmethodhandleradapter 实例
这两个实例分别在类级别和方法级别处理
而annotation-driven配置帮助我们自动完成上述的两个实例注入
-->
<mvc:annotation-driven/>
<!--视图解析器: 模板引擎 thymeleaf freemarker-->
<bean class="org.springframework.web.servlet.view.internalresourceviewresolver" id="internalresourceviewresolver">
<!--前缀-->
<property name="prefix" value="/web-inf/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
在视图解析器中我们把所有的视图都存在/web-inf/目录下,这样可以保证视图安全,因为这个目录下的文件,客户端不能直接访问
创建 controller
编写一个 java 控制类: cn.itcast.controller.hellocontroller
package cn.itcast.controller;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.requestmapping;
@controller
@requestmapping("/hellocontroller")
public class hellocontroller {
// localhost:8080//hellocontroller/hello1 --(真正访问地址)
@requestmapping("/hello1")
public string hello1(model model){
//封装数据
model.addattribute("msg","hello,springmvcanntation");
return "hello"; //会被视图解析器处理
}
//多个书写方法
/*@requestmapping("/hello2")
public string hello2(model model){
//封装数据
model.addattribute("msg","hello,springmvcanntation");
return "hello"; //会被视图解析器处理
}*/
}
编写一个 hello.jsp 用于跳转显示
<%@ page contenttype="text/html;charset=utf-8" language="java" %>
<html>
<head>
<title>title</title>
</head>
<body>
${msg} <!--接收msg参数数据-->
</body>
</html>
使用 springmvc 必须配置的三大件
处理器映射器 , 处理器适配器 , 视图解析器
通常,我们只需要 手动配置视图解析器 ,而 处理器映射器 和 处理器适配器
如对本文有疑问, 点击进行留言回复!!
VideoStudio多相机编辑器怎么用? VideoStudio多相机的使用方法
网友评论