1.web.xml 不变
2.springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd"> <!--注解映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!--注解适配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> <!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置 mvc:annotation-driven默认加载很多的参数绑定方法, 比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter 实际开发时使用mvc:annotation-driven --> <!-- <mvc:annotation-driven></mvc:annotation-driven> --> <!-- 视图解析器 (解析jsp,默认使用jstl标签,所以jstl下需要有jstl的包)--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置jsp路径的前缀 --> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- 配置jsp路径的后缀 --> <property name="suffix" value=".jsp"/> </bean> <!-- 配置Handler --> <!-- 对于注解的Handler可以单个配置 实际开发中建议使用组件扫描 --> <!-- <bean class="cn.itcast.ssm.controller.ItemsController3" /> --> <!-- 可以扫描controller、service、... 这里让扫描controller,指定controller的包 --> <context:component-scan base-package="com.meicai.ssm.controller"></context:component-scan> </beans>
springmvc.xml
3.Controller
package com.meicai.ssm.controller; import java.util.List; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import com.meicai.ssm.po.Items; /** * * * @author Administrator * */ @org.springframework.stereotype.Controller @RequestMapping("/Items") public class ItemsController3 { //@RequestMapping实现对searchItems方法和url的映射,一个方法对应一个url @RequestMapping("/searchItems") public ModelAndView searchItems() throws Exception { // TODO Auto-generated method stub List<Items> itemsList=new ArrayList<Items>(); Items items1=new Items(); items1.setName("联想笔记本chm3"); items1.setPrice(6000f); items1.setDetail("tinkpadT430"); Items items2=new Items(); items2.setName("苹果手机"); items2.setPrice(5000f); items2.setDetail("ipone7"); itemsList.add(items1); itemsList.add(items2); ModelAndView mav=new ModelAndView(); mav.addObject("itemsList", itemsList); mav.setViewName("itemsList"); return mav; } }
controller
4.调试:http://localhost:8080/springmvc04-01/Items/searchItems.action
时间: 2024-12-14 07:54:04