Use Eclipse to develop and debug Web Builder projects

Keywords: Programming JDBC Eclipse Tomcat MySQL

Use Eclipse to develop and debug Web Builder projects

Download and install the Groovy plug-in:

Download address: Groovy-Eclipse

Download and install the Tomcat plug-in:

Download address: Tomca-Eclipse

Open Eclipse and configure the Tomcat plug-in as shown below:

Create a new Java Project project

Name it WB8 and add Groovy support in the right-click menu of the project! As shown below:

Open the project property configuration window

Configure Tomcat as follows:

Enter JNDI data sources in Extra information, such as:

<Resource
        name="jdbc/wb_mysql"
        auth="Container" type="javax.sql.DataSource"
        driverClassName="com.mysql.jdbc.Driver"
        validationQuery="select 1" testWhileIdle="true" testOnBorrow="false"
        url="jdbc:mysql://127.0.0.1:3306/wb?autoReconnect=true&amp;allowMultiQueries=true&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;mysqlEncoding=utf8"
        username="root" password="XXXXXX"
        maxActive="20" maxIdle="10" maxWait="-1" />

Copy WebBuilder Application Directory

Create a new web directory under the project's root directory and copy everything in the / wb directory of the compressed file webbuilder.zip to the new web. The final directory structure is shown in the following figure:

Update the Context of the project

Update the WB8 project context information to Tomcat's tomcat8/conf/Catalina/localhost directory, as shown in the following figure:

The contents of the file wb.xml are as follows:

<Context path="/wb" reloadable="true" docBase="C:\WJW_E\WJW_DATA\OpenSource\WebBuilder\eclipse_workspace\WB8\web" workDir="C:\WJW_E\WJW_DATA\OpenSource\WebBuilder\eclipse_workspace\WB8\work" >
<!-- Extra info begin -->
<Resource
        name="jdbc/wb_mysql"
        auth="Container" type="javax.sql.DataSource"
        driverClassName="com.mysql.jdbc.Driver"
        validationQuery="select 1" testWhileIdle="true" testOnBorrow="false"
        url="jdbc:mysql://127.0.0.1:3306/wb?autoReconnect=true&amp;allowMultiQueries=true&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;mysqlEncoding=utf8"
        username="root" password="XXXXXX"
        maxActive="20" maxIdle="10" maxWait="-1" />

<!-- Extra info end -->
</Context>
 

Increase Groovy support

  1. Copy groovy-all-X.X.X.jar to WEB-INF/lib/directory
    Modify Java Build Path and add all jar files in WEB-INF/lib directory to Libraries!

As shown in the following figure:

  1. Modify the file WEB-INF/web.xml and add:
  <servlet>
    <servlet-name>GroovyServlet</servlet-name>
    <servlet-class>groovy.servlet.GroovyServlet</servlet-class>
    <init-param>
      <param-name>verbose</param-name>
      <param-value>false</param-value>
    </init-param>
    <init-param>
      <param-name>logGROOVY861</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>resource.name.regex</param-name>
      <param-value>gdo$</param-value>
    </init-param>
    <init-param>
      <param-name>resource.name.replacement</param-name>
      <param-value>groovy</param-value>
    </init-param>
         
    <load-on-startup>2</load-on-startup>
  </servlet>
	
  <servlet-mapping>
   <servlet-name>GroovyServlet</servlet-name>
   <url-pattern>*.gdo</url-pattern>
  </servlet-mapping>
  1. Write the test file WEB-INF/groovy/test.groovy. (According to the agreement, WEB-INF/groovy is the root directory of groovy file!)
def builder = new groovy.json.JsonBuilder()
builder.people {
	person {
		firstName 'Guillame'
		lastName 'Laforge'
		// Named arguments are valid values for objects too
		address(
		city: 'Paris',
		country: 'France',
		zip: 12345,
		)
		married true
		// a list of values
		conferences 'JavaOne', 'Hello'
	}
}

out <<  "General output:"+builder.toString()
out <<  "<br>\r\n Format output:"
out <<  builder.toPrettyString()

  1. Add authority judgment Use the Wb.request method or url to refer directly and then define the accessible roles in groovy to determine whether there are execution privileges! For example:
//xwl file
Wb.request({
  url: 'test.gdo',
  params: {
    foo: 'abc',
    bar: 123
  },
  success: function(response) {
    app.textarea1.setValue(response.responseText);
    Wb.info(response.responseText);
  }
});

//groovy file
def needRoles=['default','admin']*.toUpperCase()
def currentRoles = com.wb.common.Session.getRoles(request).toList()*.toUpperCase()

//Judgement role  
//Any role
if(!currentRoles.any { needRoles.contains(it) }) {
  com.wb.util.SysUtil.accessDenied(request);
}

//Or all roles 
if(!currentRoles.containsAll(needRoles)) {
  com.wb.util.SysUtil.accessDenied(request);
}
  1. Modify Java Build Path and add WEB-INF/groovy, the root directory of groovy file, to Source so that breakpoint debugging can be done!

As shown in the following figure:

Start the Web Builder project

As shown in the following figure:

Finally, open the browser

Enter http://localhost:8080/wb/, develop and debug!

Posted by a-scripts.com on Sat, 11 May 2019 02:15:14 -0700