准备工作
- 所需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";//重定向
}
(未完待续······)