рдЦреЛрдЬ рдЗрдВрдЬрди рджреНрд╡рд╛рд░рд╛ рдПрдкреНрд▓рд┐рдХреЗрд╢рди рдХреЛ рдЕрдиреБрдХреНрд░рдордгрд┐рдд рдХрд░рдиреЗ рдФрд░ рд▓реЛрдбрд┐рдВрдЧ рдХреЛ рдЧрддрд┐ рджреЗрдиреЗ рдХреЗ рд▓рд┐рдП рдЕрдиреБрд░реЛрдз рдХрд┐рдП рдЧрдП рдкреГрд╖реНрда рдореЗрдВ GWT RPC рдХрд╛ рдХреНрд░рдорд╛рдВрдХрди

рдЬреИрд╕рд╛ рдХрд┐ рдЖрдк рдЬрд╛рдирддреЗ рд╣реИрдВ, рдЦреЛрдЬ рдЗрдВрдЬрди рд╡рд╛рд╕реНрддрд╡ рдореЗрдВ рдЧрддрд┐рд╢реАрд▓ рд░реВрдк рд╕реЗ рдЙрддреНрдкрдиреНрди рдкреГрд╖реНрдареЛрдВ рдХреЛ рдкрд╕рдВрдж рдирд╣реАрдВ рдХрд░рддреЗ рд╣реИрдВ, рдФрд░ GWT рдкреГрд╖реНрда (рдПрдкреНрд▓рд┐рдХреЗрд╢рди) рдмрд╕ рдЧрддрд┐рд╢реАрд▓ рд╣реИред

рдЦреЛрдЬ рд░реЛрдмреЛрдЯ рдХреБрдЫ рд╕рд╛рдордЧреНрд░реА рдХреЛ рдЕрдиреБрдХреНрд░рдорд┐рдд рдХрд░рдиреЗ рдореЗрдВ рд╕рдХреНрд╖рдо рд╣реЛрдиреЗ рдХреЗ рд▓рд┐рдП, рдпрд╣ рд╕рд╛рдордЧреНрд░реА рдЕрдкрдиреЗ рд▓реЛрдбрд┐рдВрдЧ рдХреЗ рд╕рдордп html рдкреЗрдЬ рдореЗрдВ рдЙрдкрд▓рдмреНрдз рд╣реЛрдиреА рдЪрд╛рд╣рд┐рдП, рдФрд░ рд╡рд┐рд╢рд┐рд╖реНрдЯ GWT рдЕрдиреБрдкреНрд░рдпреЛрдЧреЛрдВ рдореЗрдВ, рд╕рд╛рдордЧреНрд░реА рдХреЛ рд╕рд░реНрд╡рд░ рд╕реЗ RPC рдЕрдиреБрд░реЛрдз рдХреЗ рдорд╛рдзреНрдпрдо рд╕реЗ рдЕрдиреБрд░реЛрдз рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИ рдФрд░ рдПрдХ рдЗрдВрдЯрд░рдлрд╝реЗрд╕ рдмрдирд╛рддрд╛ рд╣реИред
рдЗрд╕ рд╕рдорд╕реНрдпрд╛ рдХреЛ рджрд░рдХрд┐рдирд╛рд░ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП, рдЖрд╡реЗрджрди рд▓реЛрдб рдХрд░рддреЗ рд╕рдордп рд╕рд░реНрд╡рд░ рд╕реЗ рдЕрдиреБрд░реЛрдзрд┐рдд рд╕рд╛рдордЧреНрд░реА рдХреЛ рдкреГрд╖реНрда рдкрд░ рдХреНрд░рдордмрджреНрдз рдХрд░рдиреЗ рдХрд╛ рдПрдХ рддрд░реАрдХрд╛ рд▓рд╛рдЧреВ рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИред
рдЗрд╕ рдкрджреНрдзрддрд┐ рдХрд╛ рдПрдХ рдЕрддрд┐рд░рд┐рдХреНрдд рдмреЛрдирд╕ рдЖрд╡реЗрджрди рдХреА рд╢реБрд░реБрдЖрдд рдореЗрдВ рддреЗрдЬреА рд▓рд╛рдиреЗ рдХреЗ рд▓рд┐рдП рд╣реИ, рдХреНрдпреЛрдВрдХрд┐ рдбреЗрдЯрд╛ рдХреЗ рд▓рд┐рдП рд╕рд░реНрд╡рд░ рдкрд░ рдЬрд╛рдиреЗ рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рдирд╣реАрдВ рд╣реИред

рдСрдкрд░реЗрд╢рди рдХрд╛ рд╕рд┐рджреНрдзрд╛рдВрдд рдЗрд╕ рдкреНрд░рдХрд╛рд░ рд╣реИ:


рд╕реНрд░реЛрдд рдбреЗрдЯрд╛
рд╕реЗрд╡рд╛:
 package com.oshift.ui.client.forummanamegemt; import com.google.gwt.user.client.rpc.RemoteService; import com.google.gwt.user.client.rpc.RemoteServiceRelativePath; import com.oshift.ui.shared.forum.GWTForum; import java.util.ArrayList; @RemoteServiceRelativePath("forummanagementservice") public interface ForumManagementService extends RemoteService { public ArrayList<GWTForum> getForums(); } 


рд╕рд░реНрд╡рд▓реЗрдЯ:
 package com.oshift.ui.server.forummanamegemt; import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.oshift.ui.client.forummanamegemt.ForumManagementService; import com.oshift.ui.shared.forum.GWTForum; import com.oshift.ws.db.forummanagement.Forum; import com.oshift.ws.db.forummanagement.Mapper; import com.oshift.ws.ejb.ForumManagerBean; import java.util.ArrayList; import javax.ejb.EJB; import javax.servlet.ServletException; public class ForumManagementServiceImpl extends RemoteServiceServlet implements ForumManagementService { public static ForumManagementServiceImpl instance = null; //    @Override public void init() throws ServletException { super.init(); instance = this; } //-    , ,   EJB @EJB private ForumManagerBean forumBean; @Override public ArrayList<GWTForum> getForums() { ArrayList<GWTForum> res = new ArrayList<GWTForum>(); for (Forum f : forumBean.getForums()) { GWTForum toGwt = Mapper.ForumMap.toGwt(f); res.add(toGwt); } return res; } } 


рдХреНрд░рдорд╛рдВрдХрди рдХреЗ рд▓рд┐рдП DTO рдСрдмреНрдЬреЗрдХреНрдЯ:
* рдПрдХ рдорд╣рддреНрд╡рдкреВрд░реНрдг рдмрд┐рдВрджреБ, рдЕрдкрдиреЗ рдЖрдк рдХреЛ SerializationPolicy рд╕реНрдерд╛рдкрд┐рдд рдХрд░рдиреЗ рдХреЗ рджрд░реНрдж рд╕реЗ рдЫреБрдЯрдХрд╛рд░рд╛ рдкрд╛рдиреЗ рдХреЗ рд▓рд┐рдП, рдпреЗ рдСрдмреНрдЬреЗрдХреНрдЯ IsSerializable рдЗрдВрдЯрд░рдлрд╝реЗрд╕ рд▓рд╛рдЧреВ рдХрд░рддреЗ рд╣реИрдВред
 package com.oshift.ui.shared.forum; import com.google.gwt.safehtml.shared.SafeHtml; import com.google.gwt.safehtml.shared.SafeHtmlUtils; import com.google.gwt.user.client.rpc.IsSerializable; import java.util.ArrayList; public class GWTForum extends GWTBootstrapDTO implements IsSerializable { public String forumName = " "; public ArrayList<GWTTopic> topics = new ArrayList<GWTTopic>(); public GWTForum() { } @Override public String getBootstrap() { String res = "Forum name: " + forumName + "<br>"; for (GWTTopic t : topics) { res += t.getBootstrap(); } //,     SafeHtml fromString = SafeHtmlUtils.fromString(res); return fromString.asString(); } } package com.oshift.ui.shared.forum; import com.google.gwt.safehtml.shared.SafeHtml; import com.google.gwt.safehtml.shared.SafeHtmlUtils; import com.google.gwt.user.client.rpc.IsSerializable; public class GWTTopic extends GWTBootstrapDTO implements IsSerializable { public String topicName = ""; @Override public String getBootstrap() { String res = " : " + topicName + "</br>"; SafeHtml fromString = SafeHtmlUtils.fromString(res); return fromString.asString(); } } 


рд╕рд░реНрд╡рд░-рд╕рд╛рдЗрдб рдХреНрд░рдорд╛рдВрдХрди рдкреНрд░рдХреНрд░рд┐рдпрд╛ (рд╕рд╛рджрдЧреА рдХреЗ рд▓рд┐рдП, рд╕реАрдзреЗ jsp рдореЗрдВ):
 <%@page import="com.google.gwt.safehtml.shared.SafeHtmlUtils"%> <%@page import="com.oshift.ws.ejb.ForumManagerBean"%> <%@page import="javax.ejb.EJB"%> <%@page import="java.lang.reflect.Method"%> <%@page import="com.oshift.ui.client.forummanamegemt.ForumManagementService"%> <%@page import="com.google.gwt.user.server.rpc.RPC"%> <%@page import="java.util.ArrayList"%> <%@page import="com.oshift.ui.shared.forum.GWTForum"%> <%@page import="com.oshift.ui.server.forummanamegemt.ForumManagementServiceImpl"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <% //  ArrayList<GWTForum> forumsList = ForumManagementServiceImpl.instance.getForums(); //   Method m = ForumManagementService.class.getMethod("getForums"); //       - String forums = SafeHtmlUtils.fromString(RPC.encodeResponseForSuccess(m, forumsList)).asString(); //   noscript  StringBuilder noscriptSb = new StringBuilder(); for (GWTForum f : forumsList) { noscriptSb.append(f.getBootstrap()); } String noscript = noscriptSb.toString(); %> <!DOCTYPE html> <html> <head> <script type="text/javascript" language="javascript"> var forums='<%=forums%>'; </script> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Forums index</title> <link href="res/gfStyle.css" type="text/css" rel="stylesheet"/> <link href="res/login.css" type="text/css" rel="stylesheet"/> <meta name='gwt:module' content='com.oshift.ui.index=com.oshift.ui.index'> <script type="text/javascript" src="com.oshift.ui.index/com.oshift.ui.index.nocache.js"></script> </head> <body> ... <noscript> <%=noscript%> </noscript> 


рдХреНрд▓рд╛рдЗрдВрдЯ-рд╕рд╛рдЗрдб рдбрд┐рд╕реЗрд░рд▓рд╛рдЗрдЬрд╝реЗрд╢рди рдкреНрд░рдХреНрд░рд┐рдпрд╛:
 package com.oshift.ui.client; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.shared.GWT; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.rpc.SerializationException; import com.google.gwt.user.client.rpc.SerializationStreamFactory; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.Widget; import com.oshift.ui.client.forummanamegemt.ForumManagementServiceAsync; import com.oshift.ui.client.mainpage.ForumComposite; import com.oshift.ui.shared.forum.GWTForum; import java.util.ArrayList; public class IndexEntryPoint implements EntryPoint { // private final ForumManagementServiceAsync svc = ServicesFactory.getForumManagementServiceAsync(); @Override public void onModuleLoad() { //  unescape    String forums = new HTML(getForums()).getText(); SerializationStreamFactory ssf = (SerializationStreamFactory) svc; try { ArrayList<GWTForum> readObject = (ArrayList<GWTForum>) ssf.createStreamReader(forums).readObject(); for (GWTForum f : readObject) { //       process(f); } } catch (SerializationException ex) { //  Window.alert("       : " + ex); } ...    } //     private native String getForums()/*-{ return eval("$wnd.forums"); }-*/; } 


рдкреГрд╖реНрда рдкрд░ рдореМрдЬреВрдж рд╕рд╛рдордЧреНрд░реА рдЗрд╕ рддрд░рд╣ рджрд┐рдЦрд╛рдИ рджреЗрддреА рд╣реИ:
 <!DOCTYPE html> <html> <head> <script type="text/javascript" language="javascript"> //  , . var forums='//OK[7,4,6,4,5,4,3,1,3,2,1,1,["java.util.ArrayList/4159755760","com.oshift.ui.shared.forum.GWTForum/1236332786","1","com.oshift.ui.shared.forum.GWTTopic/1653537274"," 1"," 2"," 3"],0,7]'; </script> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Forums index</title> <link href="res/gfStyle.css" type="text/css" rel="stylesheet"/> <link href="res/login.css" type="text/css" rel="stylesheet"/> <meta name='gwt:module' content='com.oshift.ui.index=com.oshift.ui.index'> <script type="text/javascript" src="com.oshift.ui.index/com.oshift.ui.index.nocache.js"></script> </head> <body> ... <noscript> Forum name: 1<br> :  1&lt;/br&gt; :  2&lt;/br&gt; :  3&lt;/br&gt; </noscript> </div> </body> </html> 


рдореБрдЭреЗ рдЙрдореНрдореАрдж рд╣реИ рдХрд┐ рдореЗрд░рд╛ рд▓реЗрдЦ рдХрд┐рд╕реА рдХреЗ рд▓рд┐рдП рдЙрдкрдпреЛрдЧреА рд╣реЛрдЧрд╛ред

Source: https://habr.com/ru/post/In163917/


All Articles