SpringMVC integrates xStream and springmvcxstream
I. Introduction:
XStream can easily convert Java objects into xml and JSON objects. This blog will use springMVC to integrate and use xStream to convert xml.
Blog about xStream use: http://blog.csdn.net/zdp072/article/details/39054197
Ii. Example:
1. code structure:
2. entity class:
(1) Account
public class Account { private int id; private String name; private String email; private String address; private Birthday birthday; // getter and setter @Override public String toString() { return this.name + "#" + this.id + "#" + this.address + "#" + this.birthday + "#" + this.email; }}
(2) User
public class User {private String name;private int age;private Boolean sex;private String address;private Birthday birthday;// getter and setter@Override public String toString() { return this.name + "#" + this.age + "#" + this.sex + "#" + this.address + "#" + this.birthday.getBirthday(); }}
(3) Birthday
public class Birthday {private String birthday;public Birthday() {}// getter and setter}
3. spring Configuration:
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" xmlns: mvc = "http://www.springframework.org/schema/mvc" xsi: schemaLocation = "http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/beans http://www.sp Ringframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <! -- Add annotation driver --> <mvc: annotation-driven/> <! -- Default scan package path --> <context: component-scan base-package = "com. zdp"/> <! -- View parser --> <bean class = "org. springframework. web. servlet. view. beanNameViewResolver "> <property name =" order "value =" 1 "/> </bean> <bean class =" org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" viewClass "value =" org. springframework. web. servlet. view. jstlView "/> </bean> <! -- Xml view, xstreampolicaller, which can be converted to any form of java object --> <bean name = "xstreampolicallingview" class = "org. springframework. web. servlet. view. xml. marshallingView "> <property name =" marshaller "> <bean class =" org. springframework. oxm. xstream. xstreampolicaller "> <! -- Enable annotation --> <property name = "autodetectAnnotations" value = "true"/> <! -- Class name alias --> <property name = "aliases"> <map> <! -- The alias of the Account class is myBeans, and myBeans In the converted xml --> <entry key = "myBeans" value = "com. zdp. domain. account "/> </map> </property> <! -- Basic attribute alias --> <property name = "fieldAliases"> <map> <! -- The brithday attribute in the Account --> <entry key = "com. zdp. domain. account. birthday "value =" birthday "/> </map> </property> </bean> </beans>
4. XstreamController
/*** Use xStream to convert Java objects to XML */@ Controller @ RequestMapping ("/xstream/view ") public class XStreamController {// normal JavaBean to XML // url: http: // localhost: 8080/springmvc_xStream/xstream/view/doXMLXstream @ RequestMapping ("/doXMLXstream ") public ModelAndView doXMLJaxb2View () {ModelAndView mav = new ModelAndView ("xStreamMarshallingView"); Account account = new Account (); account. setAddress ("address"); account. setEmail ("email"); account. setId (1); account. setName ("haha"); Birthday day = new Birthday (); day. setBirthday ("2010-11-22"); account. setBirthday (day); mav. addObject (BindingResult. MODEL_KEY_PREFIX, account); return mav;} // convert the List an with the List attribute // url: http: // localhost: 8080/springmvc_xStream/xstream/view/doListXMLXstream @ RequestMapping ("/doListXMLXstream") public ModelAndView doListXMLXStreamView () {ModelAndView mav = new ModelAndView ("xstreamexternallingview "); list <Object> list = new ArrayList <Object> (); for (int I = 0; I <3; I ++) {Account account = new Account (); account. setAddress ("Beijing #" + I); account. setEmail ("email" + I + "@ 12" + I + ". com "); account. setId (1 + I); account. setName ("haha #" + I); Birthday birthday = new Birthday (); birthday. setBirthday ("2010-11-2" + I); account. setBirthday (birthday); list. add (account); User user = new User (); user. setAddress ("china GuangZhou #" + I); user. setAge (23 + I); user. setBirthday (birthday); user. setName ("jack #" + I); user. setSex (Boolean. parseBoolean (I + ""); list. add (user);} mav. addObject (list); return mav;} // convert JavaBean with Map attributes // url: http: // localhost: 8080/springmvc_xStream/xstream/view/doMapXMLXstream @ RequestMapping ("/doMapXMLXstream") public ModelAndView doDifferXMLXStreamView () {ModelAndView mav = new ModelAndView ("xStreamMarshallingView "); account account = new Account (); account. setAddress ("Guangdong"); account. setEmail ("email"); account. setId (1); account. setName ("haha"); Birthday birthday = new Birthday (); birthday. setBirthday ("2010-11-22"); account. setBirthday (birthday); User user = new User (); user. setAddress ("china GuangZhou"); user. setAge (23); user. setBirthday (birthday); user. setName ("jack"); user. setSex (true); Map <String, Object> map = new HashMap <String, Object> (); map. put ("account", account); map. put ("user", user); mav. addObject (map); return mav;} // conversion array // url: http: // localhost: 8080/springmvc_xStream/xstream/view/doArrayXMLXstream @ RequestMapping ("/doArrayXMLXstream ") public ModelAndView doArrayXMLXStreamView () {ModelAndView mav = new ModelAndView ("xStreamMarshallingView"); Account [] accountArr = new Account [2]; Account account Account = new account (); Account. setAddress ("Beijing"); account. setEmail ("email"); account. setId (1); account. setName ("haha"); Birthday birthday = new Birthday (); birthday. setBirthday ("2010-11-22"); account. setBirthday (birthday); accountArr [0] = account; account = new Account (); account. setAddress ("Shanghai"); account. setEmail ("email"); account. setId (1); account. setName ("haha"); birthday = new Birthday (); birthday. setBirthday ("2014-11-22"); account. setBirthday (birthday); accountArr [1] = account; mav. addObject (accountArr); return mav ;}}
Source code download: http://download.csdn.net/detail/zdp072/7866271
Original article: http://blog.csdn.net/ibm_hoojo/article/details/6371647
When spring MVC and Ibatis frameworks are integrated, how does one run the process?
First, your thinking is messy. MVC is an excellent idea. Spring and Ibatis are both based on the MVC idea. Don't learn spring first if you don't know about MVC. Ibatis is an excellent persistent layer framework, mainly responsible for database operations. Spring is used for highly decoupling, which is to reduce the closeness between the caller and the called. As a container, it must be in charge of the global.
I suggest you do not rush to integrate it. Take a closer look at Spring and Ibatis separately. If you do not know the concept, You can integrate it. Isn't it hard to find it. If someone else debugs you, you still won't.
Which of the following is better for SSH integration and Spring MVC?
1. Based on specific needs, it is intended for customers after all;
2. The developer habits of the project team. The human factors also have a great impact on the project progress;
3. There is no best, only the right one. Different frameworks can achieve the same effect.