基于注解的spring MVC程序

来源:互联网 发布:网络侵权 编辑:程序博客网 时间:2024/06/07 19:31

在上一篇博文的基础上进行修改

修改配置文件

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.         http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
  8.         http://www.springframework.org/schema/context  
  9.         http://www.springframework.org/schema/context/spring-context-4.2.xsd  
  10.         ">  
  11.      
  12.   
  13.     <!-- 自动装配bean -->  
  14.    <!-- 自动检测bean -->  
  15.     <context:component-scan  
  16.         base-package="com.hellospringmvc"  
  17.     ></context:component-scan>  
  18.   
  19.   
  20.     <!-- 配置处理器映射器 -->  
  21.     <!-- 使用RequestMappingHandlerMapping需要在Handler 中使用@controller标识此类是一个控制器,使用@requestMapping指定Handler方法所对应的url -->  
  22.     <bean  
  23.         class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">  
  24.     </bean>  
  25.       
  26.     <!-- 配置处理器适配器 -->  
  27.     <!-- RequestMappingHandlerAdapter,不要求Handler实现任何接口,它需要和RequestMappingHandlerMapping注解映射器配对使用,主要解析Handler方法中的形参 -->  
  28.     <bean  
  29.         class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>  
  30.       
  31.       
  32.     <!-- 配置视图解析器  
  33.         要求将jstl的包加到classpath  
  34.      -->  
  35.    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  36.       <property name="prefix" value="/WEB-INF/" />  
  37.       <property name="suffix" value=".jsp" />  
  38.    </bean>  
  39.   
  40. </beans>  

修改类

[java] view plain copy
  1. package com.hellospringmvc;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8.   
  9. import org.springframework.stereotype.Controller;  
  10. import org.springframework.ui.ModelMap;  
  11. import org.springframework.web.bind.annotation.RequestMapping;  
  12. import org.springframework.web.bind.annotation.RequestMethod;  
  13. import org.springframework.web.servlet.ModelAndView;  
  14.   
  15. @Controller  
  16. public class HelloController {  
  17.    
  18.     @RequestMapping("/queryItems")  
  19.     public ModelAndView queryItems(){  
  20.           
  21.         //商品列表  
  22.         List<Item> itemsList = new ArrayList<Item>();  
  23.           
  24.         Item items_1 = new Item();  
  25.         items_1.setName("联想笔记本");  
  26.         items_1.setPrice(6000f);  
  27.         items_1.setDetail("ThinkPad T430 联想笔记本电脑!");  
  28.           
  29.         Item items_2 = new Item();  
  30.         items_2.setName("苹果手机");  
  31.         items_2.setPrice(5000f);  
  32.         items_2.setDetail("iphone6苹果手机!");  
  33.           
  34.         itemsList.add(items_1);  
  35.         itemsList.add(items_2);  
  36.           
  37.         //创建modelAndView准备填充数据、设置视图  
  38.         ModelAndView modelAndView = new ModelAndView();  
  39.           
  40.         //填充数据  
  41.         modelAndView.addObject("itemsList", itemsList);  
  42.         //视图  
  43.         modelAndView.setViewName("helloController");  
  44.           
  45.         return modelAndView;  
  46.     }  
  47.   
  48.   
原创粉丝点击