This course is based on the eighth Chapter !!!
Non-annotated processor mapper and adapter
I. simpleurlhandlermapping
Locate Springmvc.xml File
Find the following code to comment
<!-- <bean name="/query_test.action"class=" Cn.com.mvc.controller.FruitControllerTest"></bean>-->
<!--
<bean class= "Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" ></bean>
-
and add code:
<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>
Whether the address of the test code can be accessed successfully
Two.
Create a new class under the controller package, such as
Fruitcontrollertest2.java Code implementation:
Public classFruitControllerTest2 implements Httprequesthandler {PrivateFruitsservice Fruitsservice =NewFruitsservice (); @Override Public voidHandleRequest (httpservletrequest arg0, HttpServletResponse arg1) throws Servletexception, IOException {//Demo Service Get fruit Product ListList<fruits> fruitslist =fruitsservice.queryfruitslist (); //set up model dataArg0.setattribute ("fruitslist", fruitslist); //set the Turn viewarg0.Getrequestdispatcher("/web-inf/jsp/fruits/fruit.jsp"). forward (arg0, arg1); }}
Find Springmvc.xml
Add code
Pay attention to the Red color section
<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>
To test:
Three. JSON
Modify the Fruitcontrollertest2.java file
Public classFruitControllerTest2 implements Httprequesthandler {PrivateFruitsservice Fruitsservice =NewFruitsservice (); @Override Public voidHandleRequest (httpservletrequest arg0, HttpServletResponse arg1) throws Servletexception, IOException {//Demo Service Get fruit Product ListList<fruits> fruitslist =fruitsservice.queryfruitslist (); //Convert fruitslist to JSON stringString Josninfo =Convertlisttojson (fruitslist); //Set return formatArg1.setcharacterencoding ("Utf-8"); Arg1.setcontenttype ("Application/json;charset=utf-8"); //Write JSONarg1.getwriter (). write (Josninfo); } PrivateStringConvertlisttojson(list<fruits>fruitslist) {StringBuilder Builder=NewStringBuilder (); 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 ("]"); returnbuilder.tostring (); }}
The remaining unchanged access two addresses
Note that you need to test on the browser at this time
Http://localhost:8080/SpringMVC_1/queryFruits_test3.action
Nineth Chapter: Processor Mapper and Adapter