springMVC-映射器和适配器配置(注解)

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        ">
          <!-- 视图解析器 
        	解析jsp,默认使用jstl
        -->
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
        
        <!-- 注解的Handler配置
        	可以单个配置:<bean  class="com.springmvc.controller.ItemController3"></bean>
        	但建议用组件扫描
         -->
         
		<context:component-scan base-package="com.springmvc.controller"></context:component-scan>
        <!-- 注解映射器 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
        
        <!-- 注解适配器 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
        
        <!-- 使用 <mvc:annotation-driven>可以不用配置上面两个注解bean
        	而且 <mvc:annotation-driven>默认加载了很多的参数绑定方法
         -->
        <!-- <mvc:annotation-driven></mvc:annotation-driven> -->

</bean>

ItemController.class

package com.springmvc.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.springmvc.entity.Items;



//注解开发handler

@Controller
public class ItemController3 {
    
    /*
     * @RequestMapping实现对queryItems方法和url进行映射,一个方法对应一个url
     * 一般建议将url和方法写成一样,但不强制
     */
   @RequestMapping("/queryItems")
   public ModelAndView queryItems() throws Exception{
       	List<Items> items = new ArrayList<Items>();
	//向list添加数据
	Items item = new Items();
	item.setName("联想电脑");
	item.setPrice(1200);;
	item.setDetail("电脑");
	items.add(item);
	Items item1 = new Items();
	item1.setName("苹果手机");
	item1.setPrice(1300);;
	item1.setDetail("手机");
	items.add(item1);
	//返回ModelAndview
	ModelAndView modelAndView = new ModelAndView();
	//相当与request中的setAttribute,在jsp页面用itemList取值
	modelAndView.addObject("items",items);
	//指定视图
	modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
	
	return modelAndView;
       
   }
   /*
    * 注解可以在一个类中写多个方法,而不像非注解的方法,在一个类中只能写一个方法 
    */
   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值