您的位置:首页 > 编程语言 > Java开发

搭建SpringMvc框架

2016-01-31 20:53 447 查看

一、建立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>
到这,主要的配置就已经完成啦,剩下的就是发布运行啦!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: