在方法的声明之前添加@RequestMapping注解,用于配置处理哪个路径的请求;
应该使用public权限;
返回值类型暂时使用String类型;
方法的名称可以自定义;
方法的参数列表暂时为空。
例如,可以设计为:
package cn.tedu.spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
// http://localhost:8080/springmvc01/hello.do
@RequestMapping("hello.do")
public String hello() {
System.out.println("HelloController.hello()");
return "";
}
}
完成到以上步骤时,就可以将项目部署到Tomcat中,启动Tomcat,在浏览器中输入测试网址http://localhost:8080/springmvc01/hello.do后,在浏览器应该会提示404错误,但是,在Eclipse的控制台中,应该可以看到以上hello()方法中的输出语句!
最后,如果希望访问后,能够在浏览器输出自定义的内容,可以在方法的声明之前添加@ResponseBody注解,并将期望返回的内容写在hello()方法返回的字符串中即可!例如:
package cn.tedu.spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("hello.do")
@ResponseBody
public String hello() {
System.out.println("HelloController.hello()");
return "<font color=red>Hello</font>, <font color=blue>SpringMVC</font>!!!";
}
}
【补】2. 为什么要使用SpringMVC框架
在原生的Java EE技术体系中,处理用户请求的是Servlet组件,通常情况下,每个Servlet组件处理1种请求,例如“用户注册”的请求可能由UserRegServlet来处理,“用户登录”的请求可能由UserLoginServlet来处理……在比较复杂的业务系统中,用户提交的请求的种类可能特别多,就会导致Servlet组件的数量也特别多!进而导致代码的管理难度很大,同时,在项目运行时,诸多的Servlet组件也会占用较多的内存空间……
在SpringMVC框架的设计中,就解决了以上问题,它使用1个DispatcherServlet的组件,用于接收所有请求(当然,也可以配置为某些特定的请求,例如配置为仅处理*.do的请求),以此减少Servlet组件的数量!
原本Servlet是用于处理请求的,而在SpringMVC框架中,DispatcherServlet的主要作用是接收到请求后,分发到具体处理请求的Controller组件,其本身并不处理请求!而SpringMVC中的每个Controller组件都可以有若干个处理请求的方法,也就是每个Controller组件都可以处理若干种请求,所以,即使项目很复杂,请求的种类很多,但是,Controller组件的数量并不会太多!
DispatcherServlet与Controller的关系就好比银行大厅的取号机和业务柜台的关系。
SpringMVC框架的使用比原生的Java EE更加简单!
1. 显示页面
在项目的pom.xml中添加thymeleaf和thymeleaf-spring5这2个依赖:
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring5 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
注意:同一个
groupId
下,各
artifactId
不同,但是,如果
version
的编号规则是一样的,并且,在同一个项目中需要使用不同
artifactId
对应的依赖,必须使用相同的
version
!
package cn.tedu.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
@ComponentScan("cn.tedu.spring")
public class SpringMvcConfigurer implements WebMvcConfigurer {
private String characterEncoding = "utf-8";
@Bean
public ViewResolver configViewResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setCharacterEncoding(characterEncoding);
templateResolver.setTemplateMode("HTML");
templateResolver.setCacheable(false);
templateResolver.setPrefix("/templates/");
templateResolver.setSuffix(".html");
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setCharacterEncoding(characterEncoding);
viewResolver.setTemplateEngine(templateEngine);
return viewResolver;
}
}