Nineth Chapter: Processor Mapper and Adapter

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.