SpringMvc一个简单的框架入门程序

1. springmvc框架




2.前端控制器配置

 在web.xml中进行配置,配置示例如下:
</pre><span style="font-size:14px;"><span style="color: rgb(42, 21, 171);">在web.xml</span>中进行配置,配置示例如下:</span><pre name="code" class="html"><span style="font-size:18px;"><!-- springmvc前端控制器 -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 
     contextConfigLocation配置springmvc加载的配置文件
            如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-servlet.xml
     -->
     <init-param>
    <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value>
     </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping></span>

3.Spring的配置文件

   其映射器、适配器、视图解析器配置如下:SpingMvc.xml配置

<span style="font-size:14px;"><?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:tx="http://www.springframework.org/schema/tx"
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/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-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/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <!-- 配置Handler -->
    <bean name="/queryItems.action" class="com.liheng.ssm.controller.ItemsController1"></bean>
    <!-- 配置映射器
    将bean的name作为url进行查找,需要在配置Handler时指定beanname(就是url)
     -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
    <!-- 配置适配器 ,所有的适配器都继承了HandlerAdapter接口-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
    <!-- 配置视图解析器 
      解析jsp,默认使用jstl标签,classpath下的得有jstl的包
    -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
</beans></span>

4.简单的Hander Controller示例如下

简单的测试用例

 

public class ItemsController1 implements Controller{

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		//调用service查找数据库,查询商品列表,这里使用静态数据模拟
		List<Items> listItems=  new ArrayList<Items>();
		//向list中填充静态数据
		Items item1 = new Items();
		item1.setName("联想笔记本");
		item1.setPrice(2500);
		item1.setDetail("i7处理器");
		listItems.add(item1);
		//返回modeAndView
		ModelAndView modeAndView = new ModelAndView();
		//相当于request的setAttribut,在jsp页面中通过itemsList取数据
	modeAndView.addObject("itemsList",listItems);
		//指定视图
	modeAndView.setViewName("/WEB-INF/items/showItems.jsp");
		return modeAndView;
	}
}
一个简单的SpringMvc的入门程序示例,一个简单的框架搭建

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值