专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 SpringMVC的配置文件方法:使用注解的方法

SpringMVC的配置文件方法:使用注解的方法

更新时间:2021-09-09 11:53:37 来源:动力节点 浏览801次

1.需要添加相应的jar包。

如果要配置MVC的话是需要添加Spring-web和Spring-webmvc的。如果没有可以去官网下载 https://repo.spring.io/libs-release-local/

2.创建spring.xml文件

这个是spring的配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<context:component-scan base-package="com.etc.controller"></context:component-scan>
<!-- 这是静态处理 -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
<!--  -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value='back/'></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

3.修改web.xml文件

需要加入前端控制器,DispatcherSerclet,和相关的影响。还有Filter(可不加)是一个过滤器,可以过滤编码和权限等

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>springmvc</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 前端控制器 -->
  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!--这个是在开始的时候初始化spring,xml-->
			<param-value>classpath:spring.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
<!-- 映射 -->
	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<!-- 这个是filter 可以过滤编码和权限问题 -->
	<filter>
	<filter-name>Filter</filter-name>
	<!-- 这个是 spring实现的filter -->
	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
	<init-param>
	<!-- 初始化encoding 为utf-8 -->
	<param-name>encoding</param-name>
	<param-value>UTF-8</param-value>	
	</init-param>
	<init-param>
	<!-- 这个是设置response的编码问题,这这个类里如果forceEncoding是ture 的话,response和request的编码都设置 -->
	<param-name>forceEncoding</param-name>
	<param-value>true</param-value>
	</init-param>
	</filter>
	<filter-mapping>
	<filter-name>Filter</filter-name>
	<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

4.创建创建控制类

主要有三种方法,他们会有一些不同和区别。

@RestController
public class Hello {
	@GetMapping("s")
	public String say(String name) {				
		return "hello"+name;
	}
}
@Controller
public class Hello2 {
	@GetMapping("say")
	public String say(String name) {				
		return "hello";
	}
}
@Controller
public class Hello3 {
	@GetMapping("say3")
	public ModelAndView say() {
		ModelAndView mv = new ModelAndView("hello2");
		String name="abc";
		mv.addObject("name", name);		
		return mv;
	}
}

以上就是动力节点小编介绍的"SpringMVC的配置文件方法:使用注解的方法",希望对大家有帮助,想了解更多可查看SpringMVC教程。动力节点在线学习教程,针对没有任何Java基础的读者学习,让你从入门到精通,主要介绍了一些Java基础的核心知识,让同学们更好更方便的学习和了解Java编程,感兴趣的同学可以关注一下。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>