本次课程在 第八章 基础上进行的!!!
非注解的处理器映射器和适配器
一.SimpleUrlHandlerMapping
找到springmvc.xml文件
找到下面的代码进行注释
<!-- <bean name="/query_test.action" class="cn.com.mvc.controller.FruitControllerTest"></bean> -->
<!--
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
-->
并且添加代码:
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/queryFruits_test1.action">fruitController</prop> <prop key="/queryFruits_test2.action">fruitController</prop> </props> </property> </bean> <bean id="fruitController" class="cn.com.mvc.controller.FruitControllerTest"></bean>
测试代码的地址是否可以成功访问
二.
在controller包下创建新的类,如下图
FruitControllerTest2.java代码实现:
public class FruitControllerTest2 implements HttpRequestHandler { private FruitsService fruitsService = new FruitsService(); @Override public void handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { //模拟Service获取水果商品列表 List<Fruits> fruitsList = fruitsService.queryFruitsList(); //设置模型数据 arg0.setAttribute("fruitsList", fruitsList); //设置转视图 arg0.getRequestDispatcher("/WEB-INF/jsp/fruits/fruit.jsp").forward(arg0, arg1); } }
找到springmvc.xml
添加代码
注意红颜色的部分
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/queryFruits_test1.action">fruitController</prop>
<prop key="/queryFruits_test2.action">fruitController</prop>
<prop key="/queryFruits_test3.action">fruitController2</prop>
</props>
</property>
</bean>
<bean id="fruitController2" class="cn.com.mvc.controller.FruitControllerTest2"></bean>
<!-- /queryFruits_test3.action -->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
进行测试:
三.JSON
修改FruitControllerTest2.java文件
public class FruitControllerTest2 implements HttpRequestHandler { private FruitsService fruitsService = new FruitsService(); @Override public void handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { //模拟Service获取水果商品列表 List<Fruits> fruitsList = fruitsService.queryFruitsList(); //将fruitsList转为JSON串 String josnInfo = convertListToJson(fruitsList); //设置返回格式 arg1.setCharacterEncoding("utf-8"); arg1.setContentType("application/json;charset=utf-8"); //写出JSON arg1.getWriter().write(josnInfo); } private String convertListToJson(List<Fruits> fruitsList){ StringBuilder builder = new StringBuilder(); builder.append(‘[‘); for(Fruits fruits: fruitsList){ builder.append(‘{‘); builder.append("\"name\":\"").append(fruits.getName()).append("\","); builder.append("\"price\":\"").append(fruits.getPrice()).append("\","); builder.append("\"producing_area\":\"").append(fruits.getProducing_area()).append("\","); builder.append("},"); } builder.deleteCharAt(builder.length() - 1); builder.append("]"); return builder.toString(); } }
其余不变访问二中的地址
注意此时需要在浏览器上测试
http://localhost:8080/SpringMVC_1/queryFruits_test3.action
原文地址:https://www.cnblogs.com/Mrchengs/p/9822490.html