非Ajax在SpringMVC使用
# 非Ajax的SpringMVC基本使用
注【所需要的Jar文件】:
javax.servlet:javax.servlet-api:4.0.1
javax.servlet.jsp:javax.servlet.jsp-api:2.3.3
mysql:mysql-connector-java:8.0.22
commons-beanutils:commons-beanutils:1.9.4
commons-collections:commons-collections:3.2.2
commons-logging:commons-logging:1.2
junit:junit:4.12
com.google.code.gson:gson:2.8.6
org.springframework:spring-aop:5.3.6
org.springframework:spring-aspects:5.3.6
org.springframework:spring-beans:5.3.6
org.springframework:spring-context:5.3.6
org.springframework:spring-context-support:5.3.6
org.springframework:spring-core:5.3.6
org.springframework:spring-test:5.3.6
org.springframework:spring-web:5.3.6
org.springframework:spring-webmvc:5.3.6
# 1、@RestController注解
如果希望这个控制器中所有的方法返回值都是ajax = > json,则使用@RestController注解
@RestController
public class ArticleController {
@GetMapping(value = "hello", produces = "application/json;charset=utf-8")
public String sayHello(String name) {
return "name:" + name;
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 2、@Controller注解
在默认不做其他处理的情况下,没有返回ajax请求,就是普通的转发跳转,则使用@Controller注解。
测试返回这个字符串并不是数据内容,而是一个视图!
@Controller
public class BlogController {
@GetMapping("blog")
public String getBlgo() {
return "blog: id = 1 ,title = 测试";
//return "blogList";
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 3、@GetMapping
@GetMapping
用于将HTTP
get
请求映射到特定处理程序的方法注解
具体来说,@GetMapping
是一个组合注解,是@RequestMapping(method = RequestMethod.GET)
的缩写。该注解将HTTP
Get
映射到 特定的处理方法上。
# 4、@PostMapping
@PostMapping
用于将HTTP
post
请求映射到特定处理程序的方法注解
具体来说,@PostMapping
是一个组合注解,是@RequestMapping(method = RequestMethod.POST)
的缩写。
# 5、@RequestMapping
@RequestMapping(method = RequestMethod.GET)
,这行代码即说明@GetMapping
就是@RequestMapping
附加了请求方法。同时,可以看到@GetMapping
这个注解 是spring4.3
版本引入,同时引入的还有@PostMapping
、@PutMapping
、@DeleteMapping
和@PatchMapping
,一共5个注解。
所以,一般情况下用@RequestMapping(method = RequestMethod. XXXX)
即可。
# 3、applicationContext.xml加入注解的支持
<!--注解的支持-->
<context:annotation-config></context:annotation-config>
1
2
2
<context:annotation- config/
>隐式地向 Spring
容器注册
AutowiredAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor
- 这4个
BeanPostProcessor
。如下:
# 4、applicationContext.xml组件扫描路径
另,在我们使用注解时一般都会配置扫描包路径选项:
<!-- spring 可以自动去扫描 base-package下面的包或子包下面的Java文件,如果扫描到有Spring的相关
注解的类,则把这些类注册为Spring的bean -->
<context:component-scan base-package="com.singerw"></context:component-scan>
1
2
3
2
3
# 5、applicationContext.xml配置视图解析器
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--注入prefix前缀和suffix后缀!-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
1
2
3
4
5
6
2
3
4
5
6
# 6、web.xml中SpringMVC配置
<!--1、加载SpringMVC配置-->
<!-- 这个 Spring Web 应用的前端控制器,负责处理所有的应用请求 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--1 配置springmvc的配置文件路径 -->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 将所有请求映射到 DispatcherServlet 进行处理 -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<!--2 配置url-pattern 路径 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 7、web.xml中Spring配置文件
<!--2、加载Spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
- WEB-INF里面安全级别比较高,不能直接通过浏览器访问,但是可以通过控制器转发过来进行访问。
- 解释:classpath是指 WEB-INF文件夹下的classes目录,这里存放的的部署后的文件文件,这是一个定位资源的入口。
- classpath 和 classpath区别:
- classpath:只会到你的class路径中查找文件; (src ->编译 classes)
- classpath:不仅包含class路径,还包括jar文件中(class路径)进行查找.
编辑 (opens new window)