SpringMVC的简单实现-1

1.**

很简单的实现,导入jar包

**
在这里插入图片描述
普通的servlet请求是:请求-url-pattern-交给对应的servlet去处理。
而SpringMVC不一样,本身自带servlet。
**

2.需要配置web.xml

**
通过以下配置,拦截所有的请求,交给SpringMVC

  <servlet>
  	<servlet-name>springDispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	  	<servlet-name>springDispatcherServlet</servlet-name>
  		<url-pattern>/</url-pattern>
  </servlet-mapping>

完整的web.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SpringMVCProject-one</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
   <servlet>
  	<servlet-name>springDispatcherServlet</servlet-name>
  	
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<!-- 设置xml的 路径 -->
  		<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  	
  	<!-- 设置第一启动 -->
  	<load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	  	<servlet-name>springDispatcherServlet</servlet-name>
  		<url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>

3.写java代码,配置springmvc.xml文件,前端jsp代码

jsp代码:


<a href="handler/welcome1">first springmvc1</a>

配置.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		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-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">


	<context:component-scan base-package="org.awen.handler"></context:component-scan>
	
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置视图解析器的前缀和后缀 -->
		<property name="prefix" value="/view/"></property>
		<property name="suffix" value=".jsp"></property>

	</bean>
</beans>

java代码

@Controller
@RequestMapping("handler")//这个可以写 也可以不写 
public class SpringMVCHandler {
	
	//作为映射 通过welcome1 找到这里 然后 返回 到 success1.jsp
	@RequestMapping("welcome1")
	public String welcome1() {
		return "success1";
	}
	
	
		

在WebContent下面 建立文件夹 然后success会自动增加视图解析器中的前缀和后缀prefix,和suffix,也就是 补充为view/success1.jsp
前缀:/view
后缀:jsp
在这里插入图片描述

补充说明:

/中的/代表一切请求

这个代表着值拦截后缀为.action的请求

<servlet-mapping>
  	<servlet-name>springDispatcherServlet</servlet-name>
	<url-pattern>.action</url-pattern>
</servlet-mapping>

如果要分开servlet和springMVC的话,那么就使用后缀。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值