SpringMVC环境搭建

本文详细介绍SpringMVC框架下使用注解和非注解方式配置的步骤与区别,包括前端控制器配置、资源映射、静态资源处理及控制器实现。通过对比两种配置方式,帮助读者理解SpringMVC的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用注解

1 导入jar

2 在wel.xml中配置前端控制器

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0" 
	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">
	
	<servlet>
		<servlet-name>jqk</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>	
		<!-- 修改配置文件路径和名称 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>		
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>jqk</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

如果不配置<init-param>,会寻找/WEB-INF/<servlet-name>-servlet.xml文件

3 在src下新建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: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="com.lee.controller"></context:component-scan>
        
        <!-- 注解驱动 -->
        <!-- 包含了 DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter-->                
       	<mvc:annotation-driven></mvc:annotation-driven>
       	<!-- 静态资源 -->
       	<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
       	<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
       	<mvc:resources location="/image/" mapping="/image/**"></mvc:resources>
       	<mvc:resources location="/WEB-INF/js/" mapping="/abc/**"></mvc:resources>
</beans>

4 编写控制器类

@Controller
public class DemoController {
	@RequestMapping("demo")
	public String demo() {
		System.out.println("执行demo");
		return "main.jsp";
	}
	
	@RequestMapping("demo2")
	public String demo2() {
		System.out.println("执行demo");
		return "main2.jsp";
	}
}

不使用注解

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:context="http://www.springframework.org/schema/context"
    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">
        <bean id="demo123" class="com.lee.controller.DemoController"></bean>
        <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        	<property name="urlMap">
        		<map>
        		<!-- key解析出的是访问控制器的逻辑名 -->
        			<entry key="demo" value-ref="demo123"></entry>        			
        		</map>        	
        	</property>
        </bean>        
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        	<property name="prefix" value=""></property>
        	<property name="suffix" value=".jsp"></property>
        </bean>
</beans>

控制器类

public class DemoController implements Controller{

	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
		System.out.println("执行了springmvc控制器");
		ModelAndView mav = new ModelAndView("main");//默认为转发跳转
		return mav;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值