搭建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>
相关文章推荐
- java 流程语句
- spring 测试测试
- Javaweb学习之jsp与servelet的区别
- java类介绍
- 深入理解Java内存模型(五)——锁
- Java程序向MySql数据库中插入的数据变成了问号
- Java线上应用故障排查之二:高内存占用
- java导出数据到excel
- 【NanoPi2试用体验】eclipse使用的官方提供的交叉编译器
- servlet之getRealPath获取路径问题。
- eclipse编译maven项目出错- No compiler is provided in this environment. Perhaps you are running on a JRE ra
- SpringMvc实现原理
- JAVA—基础语法(一)
- Eclipse 里面 用RMI 插件来编写 RMI server and client的实际例子
- Java 字符类型
- 深入理解Java内存模型(四)——volatile
- java的成员变量和局部变量!
- Java基础——hashcode()与equals()
- 基于java社会化海量数据采集爬虫框架搭建
- Java注解教程及自定义注解