GWT RPC 无法编译项目
这个问题是上一个问题的延伸。 GWT 无法编译项目
请查看并告诉我出了什么问题。
客户端。
Twitter.java
package in.isuru.twitter.client;
import java.util.ArrayList;
import twitter4j.Tweet;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class Twitter implements EntryPoint {
private final TwitterServiceAsync searchService = GWT.create(TwitterService.class);
public void onModuleLoad() {
// Create a Flex Table
final FlexTable flexTable = new FlexTable();
FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
flexTable.addStyleName("cw-FlexTable");
flexTable.setWidth("32em");
flexTable.setCellSpacing(5);
flexTable.setCellPadding(3);
// Add some text
cellFormatter.setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);
cellFormatter.setColSpan(0, 0, 2);
searchService.search("love", new AsyncCallback<ArrayList<Tweet>>() {
@Override
public void onFailure(Throwable caught) {
//Not yet completed.
}
@Override
public void onSuccess(ArrayList<Tweet> result) {
for (int i=0 ; i <result.size() ; i++) {
Tweet tweet = result.get(i);
String user = tweet.getFromUser();
String status = tweet.getText();
addRow(flexTable, user);
addRow(flexTable, status);
}
}});
flexTable.ensureDebugId("cwFlexTable");
RootPanel.get().add(flexTable);
}
/**
* Add a row to the flex table.
*/
private void addRow(FlexTable flexTable, String status) {
int numRows = flexTable.getRowCount();
flexTable.setText(numRows, 0, status);
flexTable.getFlexCellFormatter().setRowSpan(0, 1, numRows + 1);
}
/**
* Remove a row from the flex table.
*/
private void removeRow(FlexTable flexTable) {
int numRows = flexTable.getRowCount();
if (numRows > 1) {
flexTable.removeRow(numRows - 1);
flexTable.getFlexCellFormatter().setRowSpan(0, 1, numRows - 1);
}
}
}
TwitterService.java
package in.isuru.twitter.client;
import java.util.ArrayList;
import twitter4j.Tweet;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
@RemoteServiceRelativePath("search")
public interface TwitterService extends RemoteService {
ArrayList<Tweet> search(String searchTerm) throws IllegalArgumentException;
}
TwitterServiceAsync.java
package in.isuru.twitter.client;
import java.util.ArrayList;
import twitter4j.Tweet;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface TwitterServiceAsync {
void search(String searchTerm, AsyncCallback<ArrayList<Tweet>> callback) throws llegalArgumentException;
}
服务器端
package in.isuru.twitter.server;
import java.util.ArrayList;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Tweet;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import in.isuru.twitter.client.TwitterService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
public class TwitterServiceImpl extends RemoteServiceServlet implements TwitterService {
/**
*
*/
private static final long serialVersionUID = 1L;
ArrayList<Tweet> tweets = new ArrayList<Tweet>();
@Override
public ArrayList<Tweet> search(String searchTerm) {
// The factory instance is re-useable and thread safe.
Twitter twitter = new TwitterFactory().getInstance();
Query query = new Query(searchTerm);
QueryResult result = null;
try {
result = twitter.search(query);
} catch (TwitterException e) {
e.printStackTrace();
}
for (Tweet tweet : result.getTweets()) {
//System.out.println(tweet.getFromUser() + ":" + tweet.getText());
tweets.add(tweet);
}
return tweets;
}
}
Twitter.gwt.xml
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='twitter'>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User'/>
<!-- Inherit the default GWT style sheet. You can change -->
<!-- the theme of your GWT application by uncommenting -->
<!-- any one of the following lines. -->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
<!-- <inherits name='com.google.gwt.user.theme.standard.Standard'/> -->
<!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
<!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/> -->
<!-- Other module inherits -->
<!-- Specify the app entry point class. -->
<entry-point class='in.isuru.twitter.client.Twitter'/>
<!-- Specify the paths for translatable code -->
<source path='client'/>
<source path='shared'/>
</module>
错误是
[DEBUG] [twitter] - 验证新编译的单元 [TRACE] [twitter]
- 查找入口点类 [错误] [twitter] - “file:/E:/workspace/Twitter/src/in/isuru/twitter/client/Twitter.java”中的错误
[错误] [twitter] - 第 43 行:没有可用于类型的源代码
twitter4j.Tweet;您是否忘记继承所需的模块? [错误]
[twitter] - 错误
'文件:/E:/workspace/Twitter/src/in/isuru/twitter/client/TwitterServiceAsync.java'
[错误] [twitter] - 第 11 行:没有可用于类型的源代码
twitter4j.Tweet;您是否忘记继承所需的模块? [错误]
[twitter] - 无法找到类型“in.isuru.twitter.client.Twitter”
[错误] [twitter] - 提示:以前的编译器错误可能已经发生
此类型不可用 [错误] [twitter] - 提示:检查
来自模块的继承链;它可能不会继承
所需的模块或模块可能未添加其源路径条目
正确[错误] [twitter] - 无法从用户加载模块“twitter”
代理'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7(KHTML,例如
Gecko) Chrome/16.0.912.77 Safari/535.7' at 127.0.0.1:7901
This question is an extension of previous question. GWT Cannot compile the project
Please have a look and tell me what's wrong with it.
Client side.
Twitter.java
package in.isuru.twitter.client;
import java.util.ArrayList;
import twitter4j.Tweet;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class Twitter implements EntryPoint {
private final TwitterServiceAsync searchService = GWT.create(TwitterService.class);
public void onModuleLoad() {
// Create a Flex Table
final FlexTable flexTable = new FlexTable();
FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
flexTable.addStyleName("cw-FlexTable");
flexTable.setWidth("32em");
flexTable.setCellSpacing(5);
flexTable.setCellPadding(3);
// Add some text
cellFormatter.setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);
cellFormatter.setColSpan(0, 0, 2);
searchService.search("love", new AsyncCallback<ArrayList<Tweet>>() {
@Override
public void onFailure(Throwable caught) {
//Not yet completed.
}
@Override
public void onSuccess(ArrayList<Tweet> result) {
for (int i=0 ; i <result.size() ; i++) {
Tweet tweet = result.get(i);
String user = tweet.getFromUser();
String status = tweet.getText();
addRow(flexTable, user);
addRow(flexTable, status);
}
}});
flexTable.ensureDebugId("cwFlexTable");
RootPanel.get().add(flexTable);
}
/**
* Add a row to the flex table.
*/
private void addRow(FlexTable flexTable, String status) {
int numRows = flexTable.getRowCount();
flexTable.setText(numRows, 0, status);
flexTable.getFlexCellFormatter().setRowSpan(0, 1, numRows + 1);
}
/**
* Remove a row from the flex table.
*/
private void removeRow(FlexTable flexTable) {
int numRows = flexTable.getRowCount();
if (numRows > 1) {
flexTable.removeRow(numRows - 1);
flexTable.getFlexCellFormatter().setRowSpan(0, 1, numRows - 1);
}
}
}
TwitterService.java
package in.isuru.twitter.client;
import java.util.ArrayList;
import twitter4j.Tweet;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
@RemoteServiceRelativePath("search")
public interface TwitterService extends RemoteService {
ArrayList<Tweet> search(String searchTerm) throws IllegalArgumentException;
}
TwitterServiceAsync.java
package in.isuru.twitter.client;
import java.util.ArrayList;
import twitter4j.Tweet;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface TwitterServiceAsync {
void search(String searchTerm, AsyncCallback<ArrayList<Tweet>> callback) throws llegalArgumentException;
}
Server Side
package in.isuru.twitter.server;
import java.util.ArrayList;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Tweet;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import in.isuru.twitter.client.TwitterService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
public class TwitterServiceImpl extends RemoteServiceServlet implements TwitterService {
/**
*
*/
private static final long serialVersionUID = 1L;
ArrayList<Tweet> tweets = new ArrayList<Tweet>();
@Override
public ArrayList<Tweet> search(String searchTerm) {
// The factory instance is re-useable and thread safe.
Twitter twitter = new TwitterFactory().getInstance();
Query query = new Query(searchTerm);
QueryResult result = null;
try {
result = twitter.search(query);
} catch (TwitterException e) {
e.printStackTrace();
}
for (Tweet tweet : result.getTweets()) {
//System.out.println(tweet.getFromUser() + ":" + tweet.getText());
tweets.add(tweet);
}
return tweets;
}
}
Twitter.gwt.xml
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='twitter'>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User'/>
<!-- Inherit the default GWT style sheet. You can change -->
<!-- the theme of your GWT application by uncommenting -->
<!-- any one of the following lines. -->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
<!-- <inherits name='com.google.gwt.user.theme.standard.Standard'/> -->
<!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
<!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/> -->
<!-- Other module inherits -->
<!-- Specify the app entry point class. -->
<entry-point class='in.isuru.twitter.client.Twitter'/>
<!-- Specify the paths for translatable code -->
<source path='client'/>
<source path='shared'/>
</module>
Error is
[DEBUG] [twitter] - Validating newly compiled units [TRACE] [twitter]
- Finding entry point classes [ERROR] [twitter] - Errors in 'file:/E:/workspace/Twitter/src/in/isuru/twitter/client/Twitter.java'
[ERROR] [twitter] - Line 43: No source code is available for type
twitter4j.Tweet; did you forget to inherit a required module? [ERROR]
[twitter] - Errors in
'file:/E:/workspace/Twitter/src/in/isuru/twitter/client/TwitterServiceAsync.java'
[ERROR] [twitter] - Line 11: No source code is available for type
twitter4j.Tweet; did you forget to inherit a required module? [ERROR]
[twitter] - Unable to find type 'in.isuru.twitter.client.Twitter'
[ERROR] [twitter] - Hint: Previous compiler errors may have made
this type unavailable [ERROR] [twitter] - Hint: Check the
inheritance chain from your module; it may not be inheriting a
required module or a module may not be adding its source path entries
properly [ERROR] [twitter] - Failed to load module 'twitter' from user
agent 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like
Gecko) Chrome/16.0.912.77 Safari/535.7' at 127.0.0.1:7901
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类
twitter4j.Tweet
不能在客户端代码中使用,因为它不包含在任何继承的 GWT 模块中。您需要创建自己的传输对象类,例如client
或shared
包中的某个位置的TweetDTO
。仅应在接口TwitterServiceAsync
和TwitterService
中使用此对象。然后在您的 servlet 中,创建TweetDTO
,将所需值从Tweet
复制到TweetDTO
,然后返回TweetDTO
列表。class
twitter4j.Tweet
cannot be used in client code since it is not included in any inherited GWT module. You need to create your own transfer object class, for exampleTweetDTO
somewhere in yourclient
orshared
packages. Only this object should be used in interfacesTwitterServiceAsync
andTwitterService
. Then in your servlet, you createTweetDTO
copy required values fromTweet
toTweetDTO
and then return a list ofTweetDTO
.