SpringMvc的学习之路(一)

当前比较常见的控制层框架有SpringMvc和Struts2。但是随着时代的变迁,Struts2已经慢慢退出了历史舞台。现在公司的项目,新项目大多数是用SpringMvc,而一些老项目的维护才会使用到Struts2。


那么控制层的核心的技术,主要就是参数的绑定,页面发送数据请求,服务器再处理之后,再返回结果数据。

这里有涉及到的问题:

1.jsp靠解析?

2.SpringMvc的渲染是什么?

3.Struts2和SpringMvc的区别再哪里?

这些问题请自行补充。不再这里讲。如果后期有时间,会重新编辑。

SpringMvc的运行流程:

一个web的项目运行,首先是运行web.xml。运行到web.xml的时候,查看是否配置了SpringMvc的xml文件的位置,如果没有配置,那么默认会去找/WEB-INF/文件夹下查找xml,如果还没有,就会抛出异常。那么继续运行往下运行,当运行到SpringMvc的xml文件时候,SpringMvc中的xml会说明某一个包下,是SpringMvc的文件,那么去这个包下,找,类名开头有写@controller的文件,然后保存再内存中,实例化出来。当页面发送请求的时候,就会去内存中查找相对应的对象。然后再返回给前端控制器,将处理结果返回给客户端。

步骤1.导入包


2.编写web.xml

<!-- spirngMvc前端控制器 -->
  <servlet>
  	<servlet-name>spirngMvc0523</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	
  	<!-- 如果没有指定springMvc核心配置文件那么默认会去找/WEB-INF/+<servlet-name>中的内容 +   -servlet.xml配置文件 -->
  	<!-- 指定springMvc核心配置文件位置 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:SpringMvc.xml</param-value>
  	</init-param>
  	
  	<!-- tomcat启动的时候就加载这个servlet -->
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>spirngMvc0523</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>


其中的org.springframework.web.servlet.DispatcherServlet我也不知道是什么,再导入的架包中spring-webmvc-4.1.3.RELEASE.jar-》org.springframework.web.servlet-》DispatcherServlet.class

3.编写SpringMVC的核心文件。

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://code.alibabatech.com/schema/dubbo 
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        
        <!-- 配置@Controller注解扫描 -->
        <context:component-scan base-package="cn.itheima.controller"></context:component-scan>
        
	</bean>
	
</beans>

上面一大串不知道是啥 反正加就对了,主要是下面的配置文件包。

  4.接下来就是编写类了

@Controller
public class ItemsController {

	//指定url到请求方法的映射
	//url中输入一个地址,找到这个方法.例如:localhost:8081/springmvc0523/list.action
	@RequestMapping("/list")
	public ModelAndView  itemsList() throws Exception{
		List<Items> itemList = new ArrayList<>();
		
		//商品列表
		Items items_1 = new Items();
		items_1.setName("联想笔记本_3");
		items_1.setPrice(6000f);
		items_1.setDetail("ThinkPad T430 联想笔记本电脑!");
		
		Items items_2 = new Items();
		items_2.setName("苹果手机");
		items_2.setPrice(5000f);
		items_2.setDetail("iphone6苹果手机!");
		
		itemList.add(items_1);
		itemList.add(items_2);
		
		//模型和视图
		//model模型: 模型对象中存放了返回给页面的数据
		//view视图: 视图对象中指定了返回的页面的位置
		ModelAndView modelAndView = new ModelAndView();
		
		//将返回给页面的数据放入模型和视图对象中
		modelAndView.addObject("itemList", itemList);
		
		//指定返回的页面位置
		modelAndView.setViewName("itemList");
		
		return modelAndView;

	}
}

其中的类开头的@Controller不能忘记写,不然Springmvc的配置文件,不会把这个类加载到内存中,其中的@RequestMapping("/list"),地址可以直接访问这个方法

http://127.0.0.1:8080/项目名/list.action.再这里当请求通过了web.xml的时候,会默认添加上action。

最后struts2需要编写配置文件,而springmvc只需要映射来运行。更加利用团队的开发工作,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值