SpringMVC请求映射注解创建项目
- 新建项目,在pom.xml中添加build标签
src/main/java
/*.properties
/.xml
false
src/main/resources
**/.properties
**/*.xml
false
- 将项目添加web支持
如图
image.png
3. 给将添加lib包
image.png
4. 在web.xml中添加springmvc的配置
image.png
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 关联一个springmvc的配置文件:【servlet-name】-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- 启动级别 1-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- / 匹配所有的请求;(不包含.jsp)-->
<!-- /* 匹配所有的请求;(包括 .jsp)-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
- 添加springmvc-servlet.xml,添加配置
image.png
<!-- 自动扫描包,让指定包下的注解生效,有IOC容器统一管理 -->
<context:component-scan base-package="com.springmvc.controller"/>
<!-- 让Spring MVC 不处理静态资源 -->
<mvc:default-servlet-handler/>
<!--
支持mvc注解驱动
在spring中一般采用@RequestMapping 注解来完成映射关系
要想使@RequestMapping 注解生效
必须向上下文中注册DefaultAnnotationHandlerMapping
和一个AnnotationMethodHandlerAdapter实例
这两个实例分别在类级别和方法级别处理。
而annotation-driven配置帮助我们自动完成上述两个实例的注入。
-->
<mvc:annotation-driven/>
<!-- 视图解析器:DispatcherServlet 给他的ModelAndView -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
6. 添加controller, 通过注解方式映射请求路径 image.png package com.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/helloController")
public class HelloController {
// 真实访问地址: 项目名/helloController/hello
@RequestMapping("/hello")
public String sayHello(Model model){
// 向模型中添加属性msg与值,可以再jsp页面中取出并渲染
model.addAttribute("msg","hello,springMVC");
// 返回值就是试图的名字,即 /WEB-INF/jsp/hello.jsp
return "hello";
}
}
7. 添加jsp页面
image.png
<%@ page contentType=“text/html; UTF-8” language=“java” %>
${msg}
8. 将项目添加到tomcat容器中,然后运行项目 image.png搭建配置SpringMVC
编写测试代码
1、加入jar包:
junit junit 3.8.1 test org.springframework spring-test 4.0.6.RELEASE 2、创建spring-test的基类,该类主要用来加载配置文件,设置web环境的,然后所有的测试类继承该类即可,基类BaseTest类代码如下:package com.jjx.controller;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {“classpath:spring.xml”,“classpath:spring-mybatis.xml”})
@WebAppConfiguration(“src/main/webapp”)
public class BaseTest {
}
@RunWith(SpringJUnit4ClassRunner.class) 使用junit4进行测试
@ContextConfiguration() 加载spring相关的配置文件
@WebAppConfiguration() 设置web项目的环境,如果是Web项目,必须配置该属性,否则无法获取 web 容器相关的信息(request、context 等信息)
3、测试类,测试类只要继承上面的基类BaseTest即可,代码如下:
public class Mytest extends BaseTest {
@Resource // 自动注入
private EmpService empService;
@Test
public void test01() {
System.out.println(empService.getById(5000).getEname());
}
@Test
public void addCompletEmpInfo() {
// 员工入职日期
Date hiredate = new Date(new java.util.Date().getTime());
// 新增加的部门信息
Dept dept = new Dept(55, "临时部门", "地址不详");
// 新增加的员工信息
Emp emp = new Emp(7500, "路人甲", "临时工", 5000,
hiredate, new BigDecimal(2000), new BigDecimal(100), dept);
// 新增员工信息及对应的部门信息插入数据中
empService.addCompleteEmp(emp);
System.out.println("新增成功!");
}
}