GWT codeserver parameter - gwt

i've encountered an issue on GWT codeserver parameter in a simple scenario below,
Project structure
myapp
+src
++mypkg
---MainWindow.gwt.xml
---NextWindow.gwt.xml
+++client
----MainWindow.java
----NextWindow.java
+war
--MainWindow.html
--NextWindow.html
MainWindow.gwt.xml
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='mainwindow'>
<inherits name='com.google.gwt.user.User'/>
<entry-point class='mypkg.client.Mainwindow'/>
<source path='client'/>
</module>
MainWindow.java
package mypkg.client;
import com.google.gwt.core.client.*;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.ui.*;
public class MainWindow implements EntryPoint {
#Override
public void onModuleLoad() {
Button button = new Button("NextWindow!");
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
Window.open("/NextWindow.html", null, null);
}
});
RootPanel.get().add(button);
}
}
MainWindow.html
<!doctype html>
<html>
<head>
<title>MainWindow</title>
<script type="text/javascript" language="javascript"
src="mainwindow/mainwindow.nocache.js"></script>
</head>
<body>
<h1>Hi, MainWindow!</h1>
</body>
</html>
NextWindow.gwt.xml
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='nextwindow'>
<inherits name='com.google.gwt.user.User'/>
<entry-point class='mypkg.client.NextWindow'/>
<source path='client'/>
</module>
NextWindow.java
package mypkg.client;
import com.google.gwt.core.client.*;
import com.google.gwt.user.client.ui.*;
public class NextWindow implements EntryPoint {
#Override
public void onModuleLoad() {
RootPanel.get().add(new Label("Hi, NewLabel!"));
}
}
NextWindow.html
<!doctype html>
<html>
<head>
<title>NextWindow</title>
<script type="text/javascript" language="javascript"
src="nextwindow/nextwindow.nocache.js"></script>
</head>
<body>
<h1>Hi, NextWindow!</h1>
</body>
</html>
On Devmode launch the compiled myapp from the link,
http://127.0.0.1:8888/MainWindow.html?gwt.codesvr=127.0.0.1:9997
Click on button "NextWindow" then GWT browser plugin pops up a complaint window,
Module NextWindow need be (re)compiled!
Confirm it then a new browser window is open from the link on Prodmode,
http://127.0.0.1:8888/NextWindow.html
instead of the desired link on Devmode,
http://127.0.0.1:8888/NextWindow.html?gwt.codesvr=127.0.0.1:9997
Consequently it displays only,
Hi, NextWindow!
But the very anticipated content below doesn't show up,
Hi, NewLabel!
If we trail GWT codeserver parameter ?gwt.codesvr=127.0.0.1:9997 to the source code, the problem could be solved by sacrificing the consistency on source level between Devmode and Prodmode.
What are the preferable solutions indeed?

You can use if(GWT.isProdMode()) to check for Prodmode and Devmode, and trail the parameter if it is devmode.
That doesn't affect production mode - the gwt compiler is smart enough to just ignore devmode-code, so the devmode-block never makes it into the compiled javascript.

Related

Admin console is blank

I am trying to run the admin console but I click on the admin button a blank browser opens without anything in it. What am I missing? I have looked at the FAQs and the forum but still could not solve the issue. I have the server.properties file and I have added the sqlserver.jar to the lib
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='mysgwt'>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User' />
<inherits name="com.smartgwt.tools.SmartGwtTools" />
<inherits name="com.smartgwtpower.SmartGwtPower" />
<inherits name="com.smartgwtpower.tools.Tools" />
<!-- Specify the app entry point class. -->
<entry-point class='org.kai.client.MySGWT' />
<!-- Specify the paths for translatable code -->
<source path='client' />
<source path='shared' />
Code:
public class MySGWT implements EntryPoint {
private HLayout hLayout;
public void onModuleLoad() {
if (!GWT.isScript()) {
KeyIdentifier debugKey = new KeyIdentifier();
debugKey.setCtrlKey(true);
debugKey.setKeyName("D");
Page.registerKey(debugKey, new KeyCallback() {
public void execute(String keyName) {
SC.showConsole();
}
});
}
GWT.log("init OnLoadModule()...", null);
hLayout = new HLayout();
IButton adminButton = new IButton("Admin Console");
adminButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
com.smartgwtpower.tools.client.SCPOWER.openDataSourceConsole ();
}
});
IButton visualButton = new IButton("Visual Console");
visualButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
com.smartgwtpower.tools.client.SCPOWER.openVisualBuilder();
}
});
hLayout.setMembersMargin(20);
hLayout.addMember(adminButton);
hLayout.addMember(visualButton);
hLayout.draw();
}
}
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="MySGWT.css">
<title>MySGWT Project</title>
<script> var isomorphicDir = "mySGWT/sc/"; </script>
<script type="text/javascript" language="javascript" src="mysgwt/mysgwt.nocache.js"></script>
</head>
<body>
<!-- OPTIONAL: include this if you want history support -->
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
<!-- RECOMMENDED if your web app will not function without JavaScript enabled -->
<noscript>
<div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
Your web browser must have JavaScript enabled
in order for this application to display correctly.
</div>
</noscript>
</body>
</html>`
This is the eclipse logging
log4j:WARN No appenders could be found for logger (org.apache.jasper.compiler.JspRuntimeContext).
log4j:WARN Please initialize the log4j system properly.
[WARN] Aliased resource: file:/C:/myworkspace/mySGWT/war/mySGWT/sc/skins/Enterprise/images/button/button_Over_stretch.png==fil e:/C:/myworkspace/mySGWT/war/mysgwt/sc/skins/Enterprise/images/button/button_Over_stretch.png
[WARN] Aliased resource: file:/C:/myworkspace/mySGWT/war/mySGWT/sc/skins/Enterprise/images/button/button_Over_stretch.png==fil e:/C:/myworkspace/mySGWT/war/mysgwt/sc/skins/Enterprise/images/button/button_Over_stretch.png
[WARN] 404 - GET /mySGWT/sc/skins/Enterprise/images/button/button_Over_stretch.png (127.0.0.1) 1450 bytes

Access Javascript Library D3 from GWT

I include the d3 javascript like this
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="http://d3js.org/d3.v2.js"></script>
<script type="text/javascript" src="d3/d3.nocache.js"></script>
</head>
<body>
</body>
</html>
Then when I call it from the entry point d3 is null, why?
package com.example.client;
import com.google.gwt.core.client.EntryPoint;
public class D3 implements EntryPoint {
public void onModuleLoad() {
test();
}
private native void test()/*-{
$wnd.alert($wnd.d3);
}-*/;
}
Well, it seems that the problem was that I named the project D3. Need to call it something else because it seems that it overwrites the D3 of the javascript library.

How to access codes outside of IFRAME in GWT?

I have a html, say A.html. I am invoking A.html inside of A.html inside an IFRAME.
A.html
<html>
.....
<IFRAME>
A.html (same domain, etc as A.html)
</IFRAME>
</html>
This is a GWT app. This IFRAME is a popup, and when the user closes (clicks a close button) this popup (which is also loading the A.html as you can see above), I want to
fire an event to display some message on the outer A.html which means I need an access to codes outside of IFRAME. How can I achieve this?
Thanks
Since your iframe contents are being loaded from the same domain, you can call javascript functions on the containing page. I'm assuming that you're asking about calling the code outside the iframe from GWT. If so, then you can use JSNI.
For example, given the JSNI method:
private final native void postClosedEvent()
/*-{
$wnd.functionDefinedOutside();
}-*/
You could call the method within a GWT ClickHandler:
closeButton.addClickHandler(new ClickHandler()
{
public void onClick(ClickEvent e)
{
postClosedEvent()
}
});
This assumes a function defined using javascript in the containing page, for instance:
<script type="text/javascript">
var functionDefinedOutside =
function()
{
alert("GOT MESSAGE FROM iframe");
};
</script>
Note: If the iframe contents are loaded from a different domain, you can achieve the same result using postMessage.
IFRAME in the GWT module is defined as:
<iframe src="/temp.jsp"></iframe>
GWT module (Outer class):
public class Test implements EntryPoint {
public void onModuleLoad() {
registerMethod();
}
private static native void registerMethod() /*-{
$wnd.mt = $entry(#test.client.Test::show(Ljava/lang/String;));
}-*/;
public static void show(String value){
Window.alert("Calling from inside IFRAME " + value);
}
}
JSP inside the IFRAME: temp.jsp
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>temp</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
</style>
</head>
<body>
<div id="content">
Holla commo estas from the iframe!
</div>
<script type="text/javascript">
window.alert("inside iframe");
window.parent.mt("passing a value to a parent!!!");
</script>
</body>
</html>

Prepopulate form in Struts1

I have a jsp as my view, which displays a form for adding a new user/ updating the user, if one is selected. I can't figure out how to prepopulate my form if a user is selected. I read about the solution using 2 actions, with the same form, one of which is just used to populate the fields, and on for submitting the data.
However, this doesn't work for me, as my action (the one defined in action attribute for the form) isn't called when loading the jsp (can't really explain this either, the menu and pages are defined in an xml file). I don't understand how to specify the second action in my jsp, and how to make sure that the action is called when first loading the jsp. I would prefer a solution not involving AJAX, if possible. Thanks.
Why do you want to go for AJAX, when you have the power of Struts. I have a simple example (it is tested) for you.
MyForm.java
package com.tusar.action;
import java.io.Serializable;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
public class MyForm extends ActionForm implements Serializable{
private static final long serialVersionUID = 1043346271910809710L;
private String fullName = null;
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
/*This method will be called when you press the reset button
or load the form. You may want to populate the form here also*/
public void reset(ActionMapping mapping, HttpServletRequest request){
String reset = (String)request.getAttribute("myForm.reset");
if ((null != reset)|| ("true".equals(reset))) {
fullName = null;
}
}
}
MyFormSetupAction.java
package com.tusar.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class MyFormSetupAction extends Action{
/*Set your form-bean properties here*/
#Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
MyForm hwForm = (MyForm) form;
hwForm.setFullName("tusar");
return mapping.findForward("success");
}
}
MyFormSuccessAction.java
package com.tusar.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class MyFormSuccessAction extends Action{
#Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("success");
}
}
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<!-- ==================== Form Bean Definitions -->
<form-beans>
<form-bean name="myForm" type="com.tusar.action.MyForm">
<form-property name="fullName" type="java.lang.String"/>
</form-bean>
<!-- ============= Global Forward Definitions -->
<global-forwards>
<!-- Default forward to "Welcome" action -->
<!-- Demonstrates using index.jsp to forward -->
<forward name="home" path="/home.do"/>
</global-forwards>
<!-- ===================== Action Mapping Definitions -->
<action-mappings>
<!-- This action will load the form-->
<action path="/home"
type="com.tusar.action.MyFormSetupAction"
name="myForm"
validate="false"
input="/WEB-INF/jsp/home.jsp">
<forward name="success" path="/WEB-INF/jsp/home.jsp" />
</action>
<!-- This action will evalutae the form and pass form data to
success page-->
<action path="/successAction"
type="com.tusar.action.MyFormSuccessAction"
name="myForm"
validate="true"
input="/WEB-INF/jsp/home.jsp">
<forward name="success" path="/WEB-INF/jsp/success.jsp" />
</action>
</action-mappings>
<!-- ============= Message Resources Definitions -->
<message-resources parameter="MessageResources" />
</struts-config>
home.jsp
<%# taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%# taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# page isELIgnored="false"%>
<html>
<head>
<title>Struts 1</title>
</head>
<body>
<html:form action="/successAction.do">
Name:
<html:text property="fullName"></html:text>
<html:submit value="Next"></html:submit>
<html:reset value="Cancel"></html:reset>
</html:form>
</body>
</html>
success.jsp
<%# taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%# taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# page isELIgnored="false"%>
<html>
<head>
<title>Successful !</title>
</head>
<body>
<h3>Your details:</h3>
Name:
<bean:write name="myForm" property="fullName" />
</body>
</html>
Everytime you call the home.do action, the fullName property is populated with "tusar". Just comment if you need any further association, I'll be happy to help you. Thanks!
There should be an action called when selecting the user. The properties of that user should be copied into the action form before returning the action forward.
Without any configuration information it's difficult to help beyond that.

How should I template my site in Eclipse

I'm using Eclipse for the first time, wondering which way to go about templating it? I know a little about tiles and jsp's, zero about databases.
the site:
static header, nav, sidebar, and footer
a few different content jsp's
main question is --> I have one content section with one layout but 100's of varying jsp's...how should I go about this?
thanks
I'm not sure how Struts and databases are related to this, but basically, <jsp:include> is the best what you can get in JSP.
Basic kickoff example of /WEB-INF/template.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<title>${title}</title>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<div id="header">
header
</div>
<div id="menu">
menu
</div>
<div id="content">
<jsp:include page="/WEB-INF/${view}.jsp" />
</div>
<div id="footer">
footer
</div>
</body>
</html>
And the controller Servlet:
#WebServlet(urlPatterns={"/pages/*"})
public class Controller extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String view = request.getPathInfo().substring(1);
String title = titles.get(view); // Imagine that it's a map or something.
request.setAttribute("view", view);
request.setAttribute("title", title);
request.getRequestDispatcher("/WEB-INF/template.jsp").forward(request, response);
}
}
Invoke it by
http://localhost:8080/contextname/pages/foo
and provide a /WEB-INF/foo.jsp file which should represent the content. E.g.
/WEB-INF/foo.jsp
<h1>This is Foo!</h1>
<p>Lorem ipsum</p>