How to Use Groovy to write actions in Struts2

Source: Internet
Author: User
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>

  • Test
Start Tomcat and access http: // localhost: 8080/myapp/yyhelloworld. action

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.