Java Web 项目简单配置 Spring MVC进行访问

时间:2021-06-19 09:30:20

所需要的 jar 包下载地址:

https://download.csdn.net/download/qq_35318576/10275163

配置一:

新建 springmvc.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:tx="http://www.springframework.org/schema/tx"
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-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 自动扫描的包名 -->
<context:component-scan base-package="com.jd.yw" /> <!-- 默认的注解映射的支持,自动注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
<mvc:annotation-driven /> <!-- 视图解释类 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean> <!-- 对静态资源文件的访问 -->
<mvc:resources mapping="/images/**" location="/WEB-INF/images/" cache-period="31556926" />
<mvc:resources mapping="/js/**" location="/WEB-INF/js/" cache-period="31556926" />
<mvc:resources mapping="/css/**" location="/WEB-INF/css/" cache-period="31556926" /> </beans>

配置二:

web.xml 文件配置 SpringMVC

 <servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.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>

配置三:

新建 Controller 配置 @RequestMapping 便可以进行访问了