Because of the convenience of groovy dynamic language, there are two options to integrate in common web projects.
It is very convenient to use the groovy-based grails framework and is suitable for new projects.
Use groovy integration to maintain existing projects (after all, upgrading a project according to grails requires a lot of work for large projects)
The following describes how to maintain and use groovy for a project:
Web. xmlConfiguration: Add groovyResponse Servlet
<Servlet>
<Servlet-Name> groovy </servlet-Name>
<Servlet-class> groovy. servlet. policyservlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> groovy </servlet-Name>
<URL-pattern> *. Groovy </url-pattern>
</Servlet-mapping>
Add dependency Library
All dependencies for groovy-all-1.7.10.jar groovy
Mysql-connector-java-5.0.7-bin.jar: MySQL driver, database project use
Example
The following are simple instances that use SQL and data binding.
Importgroovy. SQL. SQL
If (! Session)
{
Session = request. getsession (true)
}
If (! Session. Counter)
{
Session. Counter = 1
}
/* Drop table if exists 'Demo'. 'text _ search ';
Create Table 'Demo'. 'text _ search '(
'Id' bigint (20) not null auto_increment,
'Version' bigint (20) not null,
'Description' varchar (255) not null,
'Username' varchar (255) not null,
Primary Key ('id '),
Unique key 'username' ('username ')
) Engine = MyISAM auto_increment = 4 default charset = Latin1;
*/
Defdb = groovy. SQL. SQL. newinstance ('jdbc: mysql: // localhost: 3306/demo', 'root', '', 'com. mySQL. JDBC. driver ')
Def name = Params. Guess
Defdesc = "descdd"
Try
{
Db.exe cute ("insert into text_search (version, username, description) values (1, $ {name}, $ {DESC })")
} Catch (exception)
{
Println 'already exists'
}
Html.html {
Head {
Title ("groovy Demo ")
}
Body {
P ("$ {request. remotehost }::: {session. Counter }")
H1 'variables in the binding :'
Table (Summary: 'binding '){
Tbody {
Binding. variables. Each {key, value->
Tr {
Tdkey. tostring ()
TD (value? Value. tostring (): 'null ')
}
}
}
}
HR ()
DB. eachrow ("select * From text_search "){
Println it. ID
Printlnit. Username
HR ()
}
}
}
Session. Counter = session. Counter + 1
Use http: // localhost: 8084/demo/Hello. Groovy? Guess = access by demo to write data and read content
For more information, see
Name |
Remarks (corresponding in servlet) |
Instance usage |
Headers |
Map type HTTP request headers |
Headers. Host |
Params |
Map type HTTP Request Parameters |
Params. myparam |
Session |
Servletsession, can be null |
Session ?. Myparam |
Request |
Httpservletrequest |
Request. remotehost |
Response |
Httpservletresponse |
Response. contenttype = 'text/xml' |
Context |
Servletcontext |
Context. myparam |
Application |
Servletcontext (same as context) |
Application. myparam |
Out |
Response. Writer |
Delayed initialization. It is initialized when it is used for the first time. |
Sout |
Response. outputstream |
Delayed initialization. It is initialized when it is used for the first time. |
Html |
Builderinitialized as new markupbuilder (out) |
Delayed initialization. It is initialized when it is used for the first time. |
Templatinggroovlets
If you want to process more service pages, you can use templates. For more information, see.
Summary
Not very familiar with Java (such.. Net personnel) maintain existing Java Web projects, and use groovy to quickly follow up. At the same time, due to the simplicity and integration of groovy syntax, for common XML file processing, data processing, and so on, you can use less conciseCodeImplementation
Groovy sample is more systematic and easy to find
And existing Java code can call each other