springMVC
第一个springmvc程序
这里我们使用注解Annotation完成。
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>
1.编写web.xml文件
<!--注册servlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--关联spring配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</init-param>
<!--启动顺序,数字越小启动越早-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--拦截所有的请求-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
/和/*的区别
<url-pattern>/</url-pattern>和<url-pattern>/*</url-pattern>的区别
使用/不会扫描到.jsp文件不会报错,使用/*会报404的错误,因为在去找dispatchServlet的时候跳转到jsp页面,所有就报错了
2.配置spring-config.xml配置文件
<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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--自动扫描包-->
<context:component-scan base-package="cn.com.scitc.controller"/>
<!--让mvc不处理静态资源-->
<mvc:default-servlet-handler/>
<!--mvc注解驱动
在spring中通常使用@RequestMappring完成关系映射,
如果想要使@RequestMappring生效需要在上下文注入
DefaultAnnotationHandlerMappring、AnnotationMethodHandlerAdapter
这两个实例,他们是类级别和方法级别的处理。annotation-driven可以完成上述两个实列
的注入。
-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3.编写controller
4.jsp页面
<%--
Created by IntelliJ IDEA.
User: 86180
Date: 2021年7月7日
Time: 09:02
To change this template use File | Settings | File Templates.
--%>
<%

浙公网安备 33010602011771号