搭建SpringMvc框架



一、建立Web项目

  在eclipse下创建动态web工程springmvc。


二、导入Spring的jar包


三、配置前端控制器

 在WEB-INF\web.xml中配置前端控制器

<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:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</span>
 

*.action拦截所有以.action为结尾的方法


四、springmvc配置文件

<span style="font-size:18px;"><init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</span>

五:配置处理器适配器

    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

     SimpleControllerHandlerAdapter:即简单控制器处理适配器,所有实现了org.springframework.web.servlet.mvc.Controller 接口的Bean作为Springmvc的后端控制器。


六、处理器开发


public class ItemList1 implements Controller {

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		//商品列表
		List<Items> itemsList = new ArrayList<Items>();
		
		Items items_1 = new Items();
		items_1.setName("联想笔记本");
		items_1.setPrice(6000f);
		items_1.setDetail("ThinkPad T430 联想笔记本电脑!");
		;
		
		//创建modelAndView准备填充数据、设置视图
		ModelAndView modelAndView = new ModelAndView();
		
		//填充数据
		modelAndView.addObject("itemsList", itemsList);
		//视图
		modelAndView.setViewName("order/itemsList");
		
		return modelAndView;
	}
	

}<span style="color:#3366ff;">
</span>


七、配置处理器映射器


在springmvc.xml文件配置:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-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/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 处理器映射器 -->
	<!-- 根据bean的name进行查找Handler 将action的url配置在bean的name中 -->
	<bean
		class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

八、处理器配置


在springmvc.xml文件配置:
</pre><pre code_snippet_id="1571385" snippet_file_name="blog_20160131_7_4653238" name="code" class="java"><span style="font-size:18px;background-color: rgb(240, 240, 240);"><!-- controller配置 -->
	<bean name="/items1.action" id="itemList1" 
class="cn.itcast.springmvc.controller.first.ItemList1"/>	</span><span style="font-size:24px;background-color: rgb(51, 102, 255);">
</span>


九、配置视图解析器

在springmvc.xml文件配置:
<!-- ViewResolver -->

<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>


到这,主要的配置就已经完成啦,剩下的就是发布运行啦!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值