P4jmvc beta version

Source: Internet
Author: User

(Declaration: currently, the p4jmvc internal beta version only supports spring MVC and is only for your technical reference .)

Attachment Source: http://download.csdn.net/detail/partner4java/5220557

Purpose: simplify the controller and perform common curd operations. The controller does not need to write a line of code, but only needs to inherit the code.

Note: p4jmvc mainly uses p4jorm.

For example, we only need to input a fixed parameter ID to modify and delete the city information controller)

Inherited from basemanageview, you only need to complete two additional points:

1. Input two generic types: entity and formbean;

2. input the parameter constructorBasemanageview p4jdao.

@Controller@RequestMapping("/manage/city/*")public class CityManageView extends BaseManageView<City, CityForm>{@SuppressWarnings("unused")private CityService cityService;@Autowiredpublic CityManageView(CityService cityService) {super(cityService);this.cityService = cityService;}}

The core functions are provided by class basemanageview (For details, refer to the source code of the attachment ):

Package COM. partner4java. MVC. controller; import Java. util. linkedhashmap; import Org. apache. commons. logging. log; import Org. apache. commons. logging. logfactory; import Org. springframework. beans. beanutils; import Org. springframework. beans. beansexception; import Org. springframework. UI. model; import Org. springframework. web. BIND. annotation. modelattribute; import Org. springframework. web. BIND. annotation. requestmapp Ing; import Org. springframework. web. BIND. annotation. requestmethod; import Org. springframework. web. BIND. annotation. requestparam; import COM. partner4java. MVC. formbean. baseform; import COM. partner4java. orm. bean. page. pagedata; import COM. partner4java. orm. bean. page. pageindex; import COM. partner4java. orm. dao. p4jdao; import COM. partner4java. orm. dao. enums. ordertype; import COM. partner4java. tools. generic. genericsg Ethelper; import com. partner4java. Tools. reflection. reflectiondatahelper;/*** Controller basic class <br/> * encapsulates the basic curd view function. Currently, it is only applicable to spring MVC. <Br/> * <B> Note: </B> the storage location and name of the view must be fixed, and the prefix of viewresolver is the root, the curd view of the object must be stored in the same directory as the object name: <br/> * The authority object is used as an example to store it as "authority/manage. JSP ", *" authority/Add. JSP "," authority/edit. JSP "** @ author partner4java ** @ Param <E> * currently managed object type (entity) * @ Param <F> * currently managed formbean object type (entity) */public class basemanageview <E, F extends baseform> {public static final string set_visible = "setvisible"; public static final string visible = "visible "; public static final string id = "ID"; public static final string delete_do = "Delete. do "; public static final string close_do =" close. do "; public static final string open_do =" open. do "; public static final string edit_do =" edit. do "; public static final string edit ="/edit "; public static final string add_do =" add. do "; public static final string add ="/Add "; public static final string view_list_do =" viewlist. do "; public static final string view_list ="/viewlist "; public static final string redirect_view_list =" Redirect: viewlist. do "; protected string entityclassnamel; protected string comment; protected string formbeanfullname; protected string view_list; protected string add; protected string edit; protected p4jdao <E> Dao; private log logger = logfactory. getlog (basemanageview. class); @ suppresswarnings ("unchecked") protected class <E> entityclass = genericsgethelper. getsupergenericsclass (this. getclass (), basemanageview. class. gettypeparameters () [0]); @ suppresswarnings ("unchecked") protected class <F> formclass = genericsgethelper. getsupergenericsclass (this. getclass (), basemanageview. class. gettypeparameters () [1]);/*** you must call the service */Public basemanageview (p4jdao <E> Dao) corresponding to the ** @ Param Dao * subclass of the parameter constructor) {super (); this. dao = Dao; this. formbeanfullname = formclass. getname (); this. entityclassfullname = entityclass. getname (); this. entityclassnamel = entityclass. getsimplename (). tolowercase (); view_list = entityclassnamel + view_list; add = entityclassnamel + Add; Edit = entityclassnamel + edit ;} /*** display default view <br/> * supports automatic query and Automatic Echo ** @ Param model * spring model object * @ Param form * Currently passed in formbean * @ return * // @ requestmapping (view_list_do) public String viewlist (model, @ modelattribute ("form") f form) {return doviewlist (model, form, null );} /*** Temporary Information execution method ** @ Param model * @ Param form * @ Param orderby * @ return */protected string doviewlist (model, F form, linkedhashmap <string, ordertype> orderby) {try {pagedata <E> pagedata = Dao. query (Form, new pageindex (form. getcurrentpage (), orderby); Model. addattribute ("pagedata", pagedata);} catch (exception e) {e. printstacktrace ();} return view_list;}/*** add view forwarding ** @ Param model * spring model object * @ return */@ requestmapping (value = add_do, method = requestmethod. get) Public String addview (model) {try {model. addattribute ("form", class. forname (formbeanfullname ). newinstance ();} catch (exception e) {e. printstacktrace ();} return add ;} /*** add object action execution ** @ Param model * spring model object * @ Param form * Currently passed in formbean * @ return management view */@ requestmapping (value = add_do, method = requestmethod. post) Public String add (model, @ modelattribute ("form") f form) {try {@ suppresswarnings ("unchecked") e entity = (e) class. forname (entityclassfullname ). newinstance (); beanutils. copyproperties (Form, entity); Dao. save (entity);} catch (exception e) {e. printstacktrace ();} return redirect_view_list ;} /*** modify view forwarding ** @ Param model * spring's model object * @ Param ID * modify the Object ID * @ return */@ requestmapping (value = edit_do, method = requestmethod. get) Public String editview (model, @ requestparam (ID) int ID) {try {e entity = Dao. get (ID); idexistexception (entity, "editview"); @ suppresswarnings ("unchecked") f form = (f) class. forname (formbeanfullname ). newinstance (); beanutils. copyproperties (entity, form); form. setid (ID); Model. addattribute ("form", form);} catch (exception e) {e. printstacktrace ();} return edit ;} /*** modify the action execution <br/> * This form must contain a "" field with the same name ** @ Param model * spring model object * @ Param form * Currently passed in formbean * @ Param ID * modify the Object ID * @ return management view */@ requestmapping (value = edit_do, method = requestmethod. post) Public string edit (model, @ modelattribute ("form") f form) {try {e entity = Dao. get (form. GETID (); idexistexception (entity, "edit"); beanutils. copyproperties (Form, entity); Dao. update (entity);} catch (beansexception e) {e. printstacktrace ();} return redirect_view_list;}/*** Delete action execution ** @ Param ID * Delete Object ID * @ return */@ requestmapping (value = delete_do, method = requestmethod. get) Public String Delete (@ requestparam ("ID") int ID) {try {Dao. delete (ID);} catch (exception e) {e. printstacktrace ();} return redirect_view_list;}/*** close this data <br/> * Note that, this object must follow a field ** @ Param ID * Data ID * @ return */@ requestmapping (value = close_do, method = requestmethod. get) Public String close (@ requestparam (ID) int ID) {try {e entity = Dao. get (ID); idexistexception (entity, "close"); If (logger. isdebugenabled () {logger. debug ("close () entity:" + entity. getclass () + "value:" + entity. tostring ();} reflectiondatahelper. invokemethod (entity, set_visible, false); Dao. update (entity);} catch (exception e) {e. printstacktrace ();} return redirect_view_list;}/*** open this data <br/> * Note that, this object must follow a field ** @ Param ID * Data ID * @ return */@ requestmapping (value = open_do, method = requestmethod. get) Public String open (@ requestparam (ID) int ID) {try {e entity = Dao. get (ID); idexistexception (entity, "open"); If (logger. isdebugenabled () {logger. debug ("open () entity:" + entity. getclass () + "value:" + entity. tostring ();} reflectiondatahelper. invokemethod (entity, set_visible, true); Dao. update (entity);} catch (exception e) {e. printstacktrace ();} return redirect_view_list;} private void idexistexception (E entity, string methodname) {If (entity = NULL) {Throw new illegalargumentexception (methodname + "ID is not exist ");}}}

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.