小编bra*_*ebg的帖子

Guice注入servlet

我是Google Guice框架的新手,我有一个关于注入guice servlet和使用RequestScope的问题.好吧,让我从我的代码中给出一些例子,只是为了清楚地说明问题.

我有一个bean类,例如Bean ..

@RequestScope
public class Bean {
    private String user;
    private String pass;

    // constructor which is @inject 

    // getters and setters
}
Run Code Online (Sandbox Code Playgroud)

在这里,我有一个servlet

@Singleton
public class MainServlet extends HttpServlet {
    doGet(HttpServletRequest request, HttpServletResponse response) {
        .... some code 
        Injector injector = Guice.createInjector();
        ValidUser validUser = injector.getInstance(ValidUser.class)
        // Here i got the below exception
    }
}



com.google.inject.ConfigurationException: Guice configuration errors:

1) No scope is bound to com.google.inject.servlet.RequestScoped.
  at Bean.class while locating Bean
Run Code Online (Sandbox Code Playgroud)

这里有趣的是,我们知道servlet范围是单例.而且我怎样才能从http请求中获取 - Bean实例?因为据我所知,在注入Bean类的实例后,它会进入http请求,对吧?

欢迎任何帮助或示例.谢谢Br

java dependency-injection guice

7
推荐指数
1
解决办法
9482
查看次数

GWT问题,GWT.create(SomeClass.class)抛出异常

我有一个gwt项目和使用方法GWT.create(SomeClass.class)抛出异常.例外是:

Loading module: Webcharge
  Top URL: http://127.0.0.1:8888/Webcharge.html?gwt.codesvr=127.0.0.1:9997
  User agent: Chrome
  Remote host: wsta1:39330
  Tab key: 
  Session key: v2aC'2^b3!lQgZS6
DEBUG: Validating newly compiled units. 
  ERROR: Errors in 'jar:file:/home/devel/Downloads/gwt-2.3.0/gwt-user.jar!/com/google/gwt/editor/client/EditorDriver.java'. 
    ERROR: Line 97: No source code is available for type javax.validation.ConstraintViolation<T>; did you forget to inherit a required module?. 
  ERROR: Errors in 'jar:file:/home/devel/Downloads/gwt-2.3.0/gwt-user.jar!/com/google/gwt/editor/client/impl/BaseEditorDriver.java'. 
    ERROR: Line 67: No source code is available for type javax.validation.ConstraintViolation<T>; did you forget to inherit a required module?. 
  ERROR: Errors in 'jar:file:/home/devel/Downloads/gwt-2.3.0/gwt-user.jar!/com/google/gwt/editor/client/impl/SimpleViolation.java'. 
    ERROR: Line 40: No source code is …
Run Code Online (Sandbox Code Playgroud)

java gwt binding deferred

5
推荐指数
2
解决办法
1万
查看次数

Java正则表达式验证特殊字符

这似乎是一个众所周知的标题,但我真的面临着一个问题.

这是我拥有的和迄今为止我所做的.

我有验证输入字符串,不允许这些字符:

&%$ ## @!〜

所以我这样编码:

 String REGEX = "^[&%$##@!~]";
 String username= "jhgjhgjh.#";
 Pattern pattern = Pattern.compile(REGEX);
 Matcher matcher = pattern.matcher(username);
 if (matcher.matches()) {
     System.out.println("matched");
 }
Run Code Online (Sandbox Code Playgroud)

java regex validation

4
推荐指数
1
解决办法
9899
查看次数

使用GWT的Gin问题 - 每次调用GWT.create(someClass.class)返回不同的实例

这是我的问题.我在一个gwt项目中使用Gin,我使用GWT.create(SomeClass.class)来获取实例,但问题是我需要signleton实例,为此我将app模块中的该类绑定为singleton.每个tome我执行GWT.create(TemplatePanel.class)它返回不同的实例..为什么?这是我的代码片段.模

public class AppClientModule extends AbstractGinModule
{
protected void configure()
{
  bind(MainPanel.class).in(Singleton.class);
  bind(TemplatePanel.class).in(Singleton.class);
}
} 
Run Code Online (Sandbox Code Playgroud)

注射器

@GinModules(AppClientModule.class)
public interface AppInjector extends Ginjector
{
  MainPanel getMainForm();
  TemplatePanel getTemplateForm();
}
Run Code Online (Sandbox Code Playgroud)

TemplatePanel

public class TemplatePanel extends VerticalPanel
@Inject
public TemplatePanel()
{
  this.add(initHeader());
  this.add(initContent());
}
..
Run Code Online (Sandbox Code Playgroud)

mainPanel中

public void onSuccess(List<MyUser> result)
    {
.......
  TemplatePanel temp = GWT.create(TemplatePanel.class);

.......
}
Run Code Online (Sandbox Code Playgroud)

和切入点

private final AppInjector injector = GWT.create(AppInjector.class);
public void onModuleLoad()
{
  MainPanel mf = injector.getMainForm();
  TemplatePanel template = injector.getTemplateForm();
  template.setContent(mf);
  RootPanel.get().add(template);
}
Run Code Online (Sandbox Code Playgroud)

java gwt gwt-gin guice

1
推荐指数
1
解决办法
1773
查看次数

GWT Guice/Gin服务器端问题

嗨,大家好问我的问题.一个GWT项目,因为我读过Gin我只能在客户端使用,而不是Guice可以在服务器端使用.这是我的问题.

我们先发布一些示例代码.

服务器端.

public class WebchargeServiceImpl extends RemoteServiceServlet implements WebchargeService
{
@Inject
private Injector injector;
@Inject
private ExecuteOperations executeOperations;

.....
executeOperations.do(); ....
Run Code Online (Sandbox Code Playgroud)

这是注入的类ExecuteOperations

@Singleton
public class ExecuteOperations
{
.........
}
Run Code Online (Sandbox Code Playgroud)

我也有servlet模块类

public class SampleWebGuiceServletConfig extends GuiceServletContextListener
{
@Override
protected Injector getInjector()
{
  return Guice.createInjector(Stage.DEVELOPMENT, new SampleWebModule());
}

} // class
Run Code Online (Sandbox Code Playgroud)

.....

public class SampleWebModule extends ServletModule
{

    @Override
    protected void configureServlets()
    {
       bind(WebchargeServiceImpl.class);  //is it correct to bind a class like that?
    } // class
Run Code Online (Sandbox Code Playgroud)

web.xml中

  <servlet>
        <servlet-name>.......WebchargeService</servlet-name>
        <servlet-class>.....WebchargeServiceImpl</servlet-class>
    </servlet> …
Run Code Online (Sandbox Code Playgroud)

java gwt gwt-gin guice code-injection

1
推荐指数
1
解决办法
1895
查看次数

GWT项目中的单身人士

有些人可以解释一下.这是一些场景.

假设我有一个类模板,并在应用程序中使用Gin/Guice.

@Singleton
public class Template extends Compose
{
private HorizontalPanel header;
private HorizontalPanel content;
private VerticalPanel menu;

    public Template()
    {
      this.add(initHeader());
      this.add(initMenu());
      this.add(initContent());
    }

    public void setContent(Widget widget)
    {
      content.clear();
      content.add(widget);
    }
    .............
    ......
    }
Run Code Online (Sandbox Code Playgroud)

在入门课堂上

........
public void onModuleLoad()
{

RootPanel.get().add(new Template());
....
}
Run Code Online (Sandbox Code Playgroud)

每次我需要重新加载我做的内容..

例如

HorizontalPanel hp = new HorizontalPanel();
hp.add ....
...

Template template = injector.getTemplate(); // return singleton instance using gin
template.setContent(hp)
Run Code Online (Sandbox Code Playgroud)

等等..

所以,模板是单例,据我所知,单个实例是每个虚拟机一个,意味着整个应用程序共享,对吧?模板类具有标题,菜单和内容,其目的是仅将内容部分重新加载为清理和添加小部件.但这是一个好方法吗?

例如,我们是否可能遇到类似用户"A"setContent(widgetA)的情况,但同时用户"B"使用方法setContent(widgetB),那么这里会发生什么?

谢谢,如果有人最终可以与我分享一个好的方法,并评论一个.

问候

java gwt singleton gwt-gin dependency-injection

1
推荐指数
1
解决办法
6731
查看次数

Java正则表达式验证\ []

我的问题是只允许a-z, A-Z, 0-9给定字符串,点,短划线和下划线以及[].

这是我的代码,但到目前为止还没有.

[a-zA-Z0-9._-]*这个可用于验证a-z, A-Z, 0-9点,破折号和下划线,但是当涉及添加和[]我得到错误非法字符. [a-zA-Z0-9._-\\[]]*显然[]打破了正则表达式.

有任何建议如何处理这个问题?

String REGEX = "[a-zA-Z0-9._-\\[]]*";
String username = "dsfsdf_12";

Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(username);

if (matcher.matches()) {
   System.out.println("matched");
} else {
  System.out.println("NOT matched");
}
Run Code Online (Sandbox Code Playgroud)

java regex

0
推荐指数
1
解决办法
216
查看次数