大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 SpringMVC的配置文件方法:使用注解的方法

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

更新時間:2021-09-09 11:53:37 來源:動力節點 瀏覽1056次

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編程,感興趣的同學可以關注一下。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 亚洲美女免费视频 | 成年女人看片免费视频频 | 久久久亚洲精品视频 | 国产婷婷一区二区三区 | 色老头xxxwww作爱视频 | 99热久久精里都是精品6 | 欧美在线一区视频 | 久久99精品麻豆国产 | 成人毛片免费 | 国产麻豆va精品视频 | 成年人黄色在线观看 | 亚洲精品久荜中文字幕 | 欧美日韩综合精品一区二区三区 | 一级淫片免费视频 | 在线视频www | 国产精品四虎在线观看免费 | 成人欧美一级毛片免费观看 | 日韩精品视频在线观看免费 | 99久久国产综合精麻豆 | 五月月色开心婷婷久久合 | 性做爰片视频毛片 | 特黄a大片免费视频 | 国产精品久久久久久亚洲伦理 | 91视频高清 | 在线亚洲精品视频 | 天天操天天摸天天干 | 久久a毛片| 在线亚洲欧洲国产综合444 | 四虎影永久在线观看网址 | 欧美一区二区三区东南亚 | 波多野结衣一区 | 啪啪一级视频 | 毛片小视频| 777午夜精品被窝影院 | 5g国产精品影院天天5g天天爽 | 国产乱在线观看视频 | 永久国产 | porno日本xxxx| 免费播放毛片 | 国产午夜亚洲精品不卡福利 | 精品日本久久久久久久久久 |