JavaWeb筆記019 SpringMVC入門

新建一個web項目,關鍵配置文件如下

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>springmvc0523</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>

	<!-- spirngMvc前端控制器 -->
	<servlet>
		<servlet-name>spirngMvc0523</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

		<!-- 如果沒有指定springMvc核心配置文件那麼默認會去找/WEB-INF/+<servlet-name>中的內容 + -servlet.xml配置文件 -->
		<!-- 指定springMvc核心配置文件位置和文件名 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:SpringMvc.xml</param-value>
		</init-param>

		<!-- tomcat啓動的時候就加載這個servlet -->
		<load-on-startup>1</load-on-startup>
	</servlet>
        <!-- 
          	*.action    代表攔截後綴名爲.action結尾的
          	/ 			攔截所有但是不包括.jsp
          	/* 			攔截所有包括.jsp
          	如果攔截的是*.html,此時返回對象,並用ResponseBody轉換成json,這時會報錯406
           -->
	<servlet-mapping>
		<servlet-name>spirngMvc0523</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
	<!-- 可以有多個攔截後綴
	<servlet-mapping>
		<servlet-name>spirngMvc0523</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
	-->
</web-app>
SpringMVC核心配置文件,文件名要和web.xml中配置的相同
<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://code.alibabatech.com/schema/dubbo 
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        
        <!-- 配置@Controller註解掃描 -->
        <context:component-scan base-package="cn.controller"></context:component-scan>
        
        <!-- 如果沒有顯示的配置處理器映射器和處理器適配那麼springMvc會去默認的dispatcherServlet.properties中查找,
        對應的處理器映射器和處理器適配器去使用,這樣每個請求都要掃描一次他的默認配置文件,效率非常低,會降低訪問速度,所以要顯示的配置處理器映射器和
        處理器適配器 -->
        
        <!-- 註解形式的處理器映射器 -->
<!--         <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->
        <!-- 註解形式的處理器適配器 -->
<!--         <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> -->
        
        <!-- 配置最新版的註解的處理器映射器 -->
<!--         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->
        <!-- 配置最新版的註解的處理器適配器 -->
<!--         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> -->

	<!-- 註解驅動:
		作用:替我們自動配置最新版的註解的處理器映射器和處理器適配器
	 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	
	<!-- 配置視圖解析器 
	作用:在controller中指定頁面路徑的時候就不用寫頁面的完整路徑名稱了,可以直接寫頁面去掉擴展名的名稱
	-->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 真正的頁面路徑 =  前綴 + 去掉後綴名的頁面名稱 + 後綴 -->
		<!-- 前綴 -->
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<!-- 後綴 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>
Controller
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.pojo.Items;

@Controller
public class ItemsController {

	//指定url到請求方法的映射
	//url中輸入一個地址,找到這個方法.例如:localhost:8081/springmvc0523/list.action
	// @RequestMapping(value = "/list",method = RequestMethod.GET) 請求的方式
	@RequestMapping("/list")
	public ModelAndView  itemsList() throws Exception{
		List<Items> itemList = new ArrayList<>();
		
		//商品列表
		Items items_1 = new Items();
		items_1.setName("聯想筆記本_3");
		items_1.setPrice(6000f);
		items_1.setDetail("ThinkPad T430 聯想筆記本電腦!");
		
		Items items_2 = new Items();
		items_2.setName("蘋果手機");
		items_2.setPrice(5000f);
		items_2.setDetail("iphone6蘋果手機!");
		
		itemList.add(items_1);
		itemList.add(items_2);
		
		//模型和視圖
		//model模型: 模型對象中存放了返回給頁面的數據
		//view視圖: 視圖對象中指定了返回的頁面的位置
		ModelAndView modelAndView = new ModelAndView();
		
		//將返回給頁面的數據放入模型和視圖對象中
		modelAndView.addObject("itemList", itemList);
		
		//指定返回的頁面路徑,如果配置文件中配置了前綴後綴,這裏可以直接寫一個頁面名字,例如itemList
		modelAndView.setViewName("/xxx/xxx/itemList.jsp");
		
		return modelAndView;

	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章