一:web.xml配置Servlet
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>spring-mybatis</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>
<!-- 配置springmvc核心控制器DispatcherServlet,springmvc的入口,springmvc本質就是servlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化參數 -->
<init-param>
<!-- 通過設置contextConfigLoaction來指定springmvc配置文件的位置 -->
<param-name>contextConfigLoaction</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- 自動啓動加載此servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 截獲並處理該項目所有URL請求 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
二:創建Spring MVC的配置文件springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 掃描包,實現註解驅動Bean的定義,同時將Bean自動注入容器中使用,使標註了SpringMvc註解的Bean生效 -->
<context:component-scan base-package="cn.mySmbms.controller"/>
<!-- 配置<mvc:annotation-driven/>完成對@Controller和@RequestMapping等註解的支持 -->
<mvc:annotation-driven/>
<!-- 配置視圖解析器,完成視圖的對應 -->
<!-- 處理請求後需要渲染輸出,這個任務由視圖實現(此處爲jsp),需要確定指定的請求需要使用哪個視圖進行請求結果的渲染輸出 -->
<!-- DispatcherServlet會查找到一個視圖解析器,將控制器返回的邏輯視圖名稱轉換成渲染結果的實際視圖 -->
<!-- 如下面定義的視圖解析器,通過配置prefix(前綴)和suffix(後綴)將視圖邏輯名解析爲/WEB-INF/jsp/<viewName>.jsp -->
<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
package cn.mySmbms.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller//可處理HTTP請求的控制器
@RequestMapping("/indexController")//避免@RequestMapping衝突
public class IndexController {
//表示方法與那個請求URL來對應,此處的URL爲/index,限定了index()將處理所有來自於/index”的請求(相對於web容器部署根目錄)
//參數傳遞:required表示參數是否必須,默認true
@RequestMapping("/index")
public ModelAndView index(@RequestParam(value="username",required=false) String username) {
System.out.println("index()執行,username:"+username);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("username",username);//返回前端的模型數據,前端獲取${username}
modelAndView.setViewName("index");//指定邏輯視圖名
return modelAndView;
}
//Model對象入參(Map也行)
/*public String index(@RequestParam(value="username",required=false) String username,Model model) {
System.out.println("index()執行,username:"+username);
model.addAttribute("username",username);//前端獲取${username}
model.addAttribute(username);//key爲對象的類型,此處爲String,前端獲取${string},javaBean同理
return "index";//邏輯視圖名
}*/
}
目錄:/WEB-INF/jsp/index.jsp
五:訪問測試
URL:http://localhost:6060/spring-mybatis/indexController/index?username=admin