搭建springMVC

配置要點:

配置前端控制器(一箇中心)

配置處理器對映器,處理器介面卡,檢視解析器(三個基本點)

掃描@Controller

1.配置web.xml檔案

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 前端控制器,DispatcherServlet讀取配置檔案 -->
<servlet>
<servlet-name>console</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 若不配的話,預設找 WEB-INF/[servlet的名稱]-servlet.xml -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-console.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 對映 -->
<servlet-mapping>
<!-- /  :攔截所有請求 (但不包括.jsp)   .js .png .css   (配置對靜態資源放行)
/* :攔截所有請求   真全攔截   (基本上不用)   
*.do  : .do 
-->
<servlet-name>console</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

2.配置Springmvc-console

<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" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
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://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://code.alibabatech.com/schema/dubbo        
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 掃描 @Controller -->	
<context:component-scan base-package="cn.itcast" />
<!-- 處理器 對映器  介面卡 -->
<mvc:annotation-driven/>
<!-- 檢視直譯器 jsp -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前字尾 -->
<property name="prefix" value="/WEB-INF/console/"/>
<property name="suffix" value=".jsp"/>
</bean>
<import resource="dubbo-consumer.xml"/>
</beans>

 

3測試springmvc

3.1建立jsp頁面

.

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
成功
</body>
</html>

3.2 寫controller類

/**
* 後臺管理
* @author yh
*
*/
@Controller
public class CenterController {
@Autowired
private TestTbService testTbService;
//入口
/**
* ModelAndView  : 跳轉檢視 資料放到一個類裡     不用
* void  : 非同步時 ajax    
* String  : 跳轉檢視         Model(檢視跟資料分開)
*/
@RequestMapping(value = "/test/index.do")
public String index(Model model){
return "index";
}
}

啟動tomcat容器

顯示測試頁面

若有問題歡迎大家與我互動交流,可評論,可留言,希望我們大家能一起學習,共同進步。