準備工作
- 所需jar包
- 在web.xml中配置springMVC的設置
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
其中:
DispatcherServlet
前端控制器,DispatcherServlet是前置控制器,配置在web.xml文件中的。攔截匹配的請求,Servlet攔截匹配規則要自已定義,把攔截下來的請求,依據相應的規則分發到目標Controller來處理,是配置spring MVC的第一步。
< param-value>classpath:springmvc-servlet.xml < /param-value>爲具體配置文件名。
url-pattern意思所有的/請求都將由springMVC處理。在src下添加springmvc-servlet.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- beans中所有連接可以由API中獲取 -->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- scan the package and the sub package 本項目中控制器所在包 -->
<context:component-scan base-package="gd"></context:component-scan>
<!-- don't handle the static resource -->
<mvc:default-servlet-handler />
<mvc:annotation-driven /><!--通常如果我們希望通過註解的方式來進行Spring MVC開發,我們都會在***-servlet.xml中加入<mvc:annotation-driven/>標籤來告訴Spring我們的目的-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
其中:
< mvc:default-servlet-handler />< mvc:annotation-driven />的介紹請參考另一篇博文:
在WEB-INF文件夾下創建名爲jsp的文件夾,用來存放jsp視圖。
創建一個hello.jsp,在body中添加“hello,springMVC”。建立包及Controller,如下所示
編寫controller類
@Controller
@RequestMapping("/myMVC")
public class MyController {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}
啓動服務器,鍵入 http://localhost:8080/項目名/myMVC/hello
最終結果如下圖所示:
向前臺傳遞數據
1、建立Person實體類
2、在原來代碼中添加如下幾行。
@RequestMapping("/hello")
public String hello(Model model){
Person p = new Person();
p.setName("張三");
p.setSex("男");
p.setAge(27);
model.addAttribute("person", p);
return "hello";
}
即可在前臺獲取Person對象中的數據。
3、修改前端JSP頁面代碼。
<body>
<h1>hello,springMVC</h1>
<p>我是${person.name},${person.sex},今年${person.age}歲</p>
</body>
運行結果:
- form提交數據
1、JSP頁面增加代碼如下:
<form action="myMVC/save" method="post">
姓名:<input name="name" type="text">
性別:<input name="sex" type="text">
年齡:<input name="age" type="text">
<input type="submit" value="提交">
</form>
2、後臺通過@ModelAttribute註解拿到Person實體類。
@RequestMapping("save")
public String save(@ModelAttribute("person") Person person,Model model){
personService.insert(person);//通過JDBC工具類存入數據庫
List<Person> personsList = personService.getAll();//通過JDBC工具類獲取數據庫所有數據
model.addAttribute("personList", personsList);
return "redirect:hello";//重定向
}
(未完待續······)