When we are working on the Struts2 project, if we update the Java program (here mainly refers to Action), we need to restart Tomcat or Web Context. Such development sometimes affects work efficiency. Every day, there is a lot of time to spend on it. After work, I think of a way to use Groovy as the Struts2 Action.
Here, I use Struts2 + Spring + policy1.0 to create a column.
- First, you need to add a Groovy jar package, including groovy, asm, and anlr packages. For specific information, refer to spring2.0-reference.pdf.
- Assume that the development environment of struts2 + srping has been configured.
- Use the Groovy language to write an Action, as follows (GroovyHelloWorldAction. groovy) import com. opensymphony. xwork2.ActionSupport;
Class policyhelloworldaction extends ActionSupport ...{
Static final String MESSAGE = "This is Groovy Hello World! ";
String message;
Public String execute () throws Exception ...{
Message = MESSAGE;
Return SUCCESS;
}
}
- Specify this Groovy Action bean in the spring configuration file, as shown below <? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xmlns: lang = "http://www.springframework.org/schema/lang"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
Http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"
Default-lazy-init = "true">
<Lang: groovy id = "yyhelloworldaction"
Script-source = "classpath: GroovyHelloWorldAction. groovy"
Scope = "prototype">
</Lang: groovy>
</Beans>
Pay attention to the schema reference.
- Add configuration in Strugs configuration file
<Action name = "yyhelloworld" class = "groovyHelloWorldAction">
<Result>/helloworld. jsp </result>
</Action>
- Add a helloworld. jsp page with the following content:
<%... @ Taglib prefix = "s" uri = "/struts-tags" %>
<Html>
<Head>
<Title> Groovy Hello World! </Title>
</Head>
<Body>
<H2> <s: property value = "message"/> </Body>
</Html>
Start Tomcat and access http: // localhost: 8080/myapp/yyhelloworld. action