要将Groovy与Spring MVC结合使用,首先确保您已经配置了Spring框架和Spring MVC。然后,您可以按照以下步骤进行操作:
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>3.0.8</version>
</dependency>
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
@Controller
class MyGroovyController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
String hello() {
return "Hello, Groovy!"
}
}
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.example.controllers"})
public class MvcConfig implements WebMvcConfigurer {
}
@Configuration
public class GroovyConfig {
@Bean
public GroovyMarkupConfigurer groovyMarkupConfigurer() {
GroovyMarkupConfigurer configurer = new GroovyMarkupConfigurer();
configurer.setResourceLoaderPath("/WEB-INF/groovy/");
return configurer;
}
}
<mvc:view-controller path="/hello" view-name="hello" />
<!DOCTYPE html>
<html>
<head>
<title>Hello Groovy</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
通过以上步骤,您就可以使用Groovy与Spring MVC结合来开发Web应用程序。您可以创建更多的Groovy Controller类,并在Spring MVC中配置它们,以实现更复杂的功能。