2

我想了解如何在 Wicket 1.5 中使用 Guice Persist (Guice 3.0)。

我找不到任何解释如何做到这一点的“hello world”类型的例子,如果你可以链接/提供这样一个很好的例子,并且很高兴地被接受为答案。

与此同时,我将尝试自己创建一个“hello world”类型的示例,随着我的进展将代码发布在这里。帮助使我的代码正常运行也将被接受为答案。


我建立了一个简单的 wicket 项目,非常类似于 Wicket 示例中的 'hello world' guice 示例,它使用 guice 进行依赖注入。我现在想扩展这个项目以使用 JPA 和 Guice Persist,而不是“Hello World”,我想从数据库中获取用户并显示其用户名。我正在尝试使用 Guice wiki 关于Guice persist的说明来实现这一点。

更新:所以,我有点让它工作了。在WebApplication.init()我注入这样的 ServetModulegetComponentInstantiationListeners().add(new GuiceComponentInjector(this, new MyServletModule()));时,我还在 web.xml 文件的顶部添加了 GuiceFilter,在 wickets 过滤器之前。

现在,当我运行应用程序时,一切正常,但我收到了关于使用不推荐使用的方法的警告。将进一步研究这一点。

警告:您正在尝试使用已弃用的 API(特别是尝试在急切创建的单例中 @Inject ServletContext。虽然我们允许这样做是为了向后兼容,但请注意,如果您有多个注入器(使用 ServletModule ) 在同一个 JVM 中运行。有关详细信息,请参阅 http://code.google.com/p/google-guice/wiki/Servlets上的 Guice 文档。

目录树

.
├── pom.xml
└── src
    └── main
        ├── java
        │   └── se
        │       └── lil
        │           ├── HomePage.html
        │           ├── HomePage.java
        │           ├── MyServletModule.java
        │           ├── WicketApplication.java
        │           ├── domain
        │           │   └── User.java
        │           └── service
        │               ├── IService.java
        │               └── JpaService.java
        ├── resources
        │   ├── META-INF
        │   │   └── persistence.xml
        │   └── log4j.properties
        └── webapp
            └── WEB-INF
                └── web.xml

WicketApplication.java

public class WicketApplication extends WebApplication {
    @Override
    protected void init() {
        super.init();
        getComponentInstantiationListeners().add(new GuiceComponentInjector(this,
                new MyServletModule()));
    }

    @Override
    public Class<? extends Page> getHomePage() {
        return se.lil.HomePage.class;
    }
}

主页.java

public class HomePage extends WebPage {
    private static final long serialVersionUID = -918138816287955837L;

    @Inject
    private IService service;

    private IModel<User> model = new LoadableDetachableModel<User>() {
        private static final long serialVersionUID = 1913317225318224531L;

        @Override
        protected User load() {
            return service.getUser();
        }
    };

    public HomePage() {
        setDefaultModel(new CompoundPropertyModel<User>(model));
        add(new Label("name"));
    }
}

主页.html

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:wicket="http://wicket.apache.org">
<head>
<title>Wicket Examples - guice</title>
</head>
<body>
    <hr />
        Value: <b wicket:id="name">name goes here</b> <br />
    <hr />
</body>
</html>

MyServletModule.java

public class MyServletModule extends ServletModule {
    protected void configureServlets() {
        install(new JpaPersistModule("manager1"));
        filter("/*").through(PersistFilter.class);
    }
}

IService.java

@ImplementedBy(JpaService.class)
public interface IService {
    public User getUser();
}

JpaService.java

public class JpaService implements IService {
    @Inject
    private EntityManager em;

    @Override
    @Transactional
    public User getUser() {
        Query q = em.createQuery("FROM User");
        q.setMaxResults(1);
        User u = (User) q.getSingleResult();
        return u;
    }
}

用户.java

@Entity
@Table (name = "users")
public class User {
    private Long id;
    private String name;

    public User() {
    }

    public User(String name) {
        this.name = name;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }
    @SuppressWarnings("unused")
    private void setId(Long id) {
        this.id = id;
    }

    @Basic
    @Column(name = "name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }   
}

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <display-name>wicketwithguice</display-name>

    <filter>
        <filter-name>guiceFilter</filter-name>
        <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>guiceFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>wicket.wicketwithguice</filter-name>
        <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
        <init-param>
            <param-name>applicationClassName</param-name>
            <param-value>se.lil.WicketApplication</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>wicket.wicketwithguice</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

持久性.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
    <persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>se.lil.domain.User</class> 
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="validate"/>
            <property name="hibernate.show_sql" value="true"/>

            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/test"/>
            <property name="javax.persistence.jdbc.user" value="test"/>
            <property name="javax.persistence.jdbc.password" value="1234"/>
        </properties>
    </persistence-unit>
</persistence>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>se.lil</groupId>
    <artifactId>wicketwithquice</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <!-- TODO project name -->
    <name>quickstart</name>
    <description></description>
    <!-- TODO <organization> <name>company name</name> <url>company url</url> </organization> -->

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <hibernate-core.version>3.6.4.Final</hibernate-core.version>
        <mysql-connector-java.version>5.1.16</mysql-connector-java.version>
        <slf4j.version>1.6.1</slf4j.version>
        <log4j.version>1.6.1</log4j.version>
        <guice.version>3.0</guice.version>
        <wicket.version>1.5.2</wicket.version>
    </properties>

    <dependencies>
        <!--GUICE DEPENDENCIES -->
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>${guice.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject.extensions</groupId>
            <artifactId>guice-servlet</artifactId>
            <version>${guice.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject.extensions</groupId>
            <artifactId>guice-persist</artifactId>
            <version>${guice.version}</version>
        </dependency>
        <!-- HIBERNATE DEPENDENCIES -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate-core.version}</version>
        </dependency>
        <!-- MYSQL DEPENDENCIES -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector-java.version}</version>
        </dependency>
        <!-- WICKET DEPENDENCIES -->
        <dependency>
            <groupId>org.apache.wicket</groupId>
            <artifactId>wicket-core</artifactId>
            <version>${wicket.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.wicket</groupId>
            <artifactId>wicket-guice</artifactId>
            <version>${wicket.version}</version>
        </dependency>

        <!-- LOGGING DEPENDENCIES - LOG4J -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${log4j.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <optimize>true</optimize>
                    <debug>true</debug>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.8</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>Apache Nexus</id>
            <url>https://repository.apache.org/content/repositories/snapshots/</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>
4

4 回答 4

1

Wicket 1.5.3 和 Guice 3 工作。对于 wicket wicket-ioc,除了 wicket core 和 wicket-guice 之外,还使用了 wicket-util 和 wicket-dev-utils。在 Netbeans7.0 中下载并创建了一个库并添加到 Web 项目中。

于 2012-03-19T00:35:00.487 回答
1

你应该看看这篇文章和评论:

http://blog.yanivkessler.com/2010/05/wicket-and-guice-alternate-route.html

于 2011-10-27T07:51:26.260 回答
0

当从 ServletContext 获取 Injector 并将其添加到 GuiceComponentInjector 时,不会出现此类消息。此处提供的 AppListener 和其他代码:

package cookbook;

import com.google.inject.persist.jpa.*;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.google.inject.persist.PersistFilter;
public class AppListener extends GuiceServletContextListener{
    @Override
    public Injector getInjector() {
    return Guice.createInjector(
      new  JpaPersistModule("WickGui3PU"  ),
      new ServletModule() {
        @Override
        protected void configureServlets() {



          filter("/*").through(PersistFilter.class);


          bind(BookDAO.class).to (BookDAOImpl.class);
        }
    });
    } 
}

在 web.xml 中添加

<listener>
      <listener-class>cookbook.AppListener</listener-class>
  </listener>

WicketApplication 中的 init() 应如下所示:

 protected void init() {
        super.init();

                Injector injector = (Injector)getServletContext().getAttribute(Injector.class.getName());

        getComponentInstantiationListeners().add(new GuiceComponentInjector(this,injector));
    }
于 2012-03-20T03:47:30.040 回答
0

当 guice 与 shiro 集成时会出现相同的消息,因此以下邮件列表可能有用“http://mail-archives.apache.org/mod_mbox/shiro-user/201108.mbox/%3C4E454692. 8090806@peachjean.com%3E"

于 2012-03-19T02:03:27.783 回答