Author: limpbizkit
Date: Fri Jan 2 18:05:17 2009
New Revision: 794
Added:
wiki/Struts2Integration.wiki
Log:
Created wiki page through web user interface.
Added: wiki/Struts2Integration.wiki
==============================================================================
--- (empty file)
+++ wiki/Struts2Integration.wiki Fri Jan 2 18:05:17 2009
@@ -0,0 +1,64 @@
+=Struts 2 Integration=
+To install the Guice Struts 2 plugin with Struts 2.0.6 or later, simply
include `guice-struts2-plugin-1.0.jar` in your web application's classpath
and select Guice as your ObjectFactory implementation in your `struts.xml`
file:
+{{{
+<constant name="struts.objectFactory" value="guice" />
+}}}
+Guice will inject all of your Struts 2 objects including actions and
interceptors. You can even scope your actions. You can optionally specify a
`Module` for Guice to install in your `struts.xml` file:
+{{{
+<constant name="guice.module" value="mypackage.MyModule"/>
+}}}
+If all of your bindings are implicit, you can get away without defining a
module at all.
+==A Counting Example==
+Say for example that we want to count the number of requests in a session.
Define a `Counter` object which will live on the session:
+{{{
+...@sessionscoped
+public class Counter {
+
+ int count = 0;
+
+ /** Increments the count and returns the new value. */
+ public synchronized int increment() {
+ return count++;
+ }
+}
+}}}
+Next, we can inject our counter into an action:
+{{{
+public class Count {
+
+ final Counter counter;
+
+ @Inject
+ public Count(Counter counter) {
+ this.counter = counter;
+ }
+
+ public String execute() {
+ return SUCCESS;
+ }
+
+ public int getCount() {
+ return counter.increment();
+ }
+}
+}}}
+Then create a mapping for our action in our struts.xml file:
+{{{
+<action name="Count"
+ class="mypackage.Count">
+ <result>/WEB-INF/Counter.jsp</result>
+</action>
+}}}
+And a JSP to render the result:
+{{{
+<%@ taglib prefix="s" uri="/struts-tags" %>
+
+<html>
+ <body>
+ <h1>Counter Example</h1>
+ <h3><b>Hits in this session:</b>
+ <s:property value="count"/></h3>
+ </body>
+</html>
+}}}
+We actually made this example more complicated than necessary in an
attempt to illustrate more concepts. In reality, we could have done away
with the separate `Counter` object and applied `...@sessionscoped` to our
action directly.
\ No newline at end of file
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-guice-dev?hl=en
-~----------~----~----~----~------~----~------~--~---