Spring MVC——搭建步骤(注解) ☆常用方式

☆常用方式

1. 搭建web工程

2. 导入jar包

spring-beans.jar、spring-context.jar、spring-core.jar、spring-expression.jar、spring-aop.jar、【spring-web.jar、spring-webmvc.jar】

添加依赖包: commons-logging.jar、aopalliance.jar

3.在web.xml中配置前端控制器DispatcherServlet:负责处理请求与响应

<!-- 配置DispatcherServlet:负责处理请求与响应 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置spring-mvc.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value><!-- 类路径 -->
<!--<param-value>/WEB-INF/spring-mvc.xml</param-value> 相对路径,默认路径:/WEB-INF/springmvc-servlet.xml -->
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

★★★★★注意:需同时配置初始化参数contextConfigLocation,确认在初始化DispatcherServlet的同时,加载spring-mvc.xml配置文件★★★★

4. 在类路径中创建spring-mvc.xml文件,配置【注解】处理器适配器、处理器映射器

<!-- 1. 【注解】处理器适配器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> -->

<!-- 1. 【注解】处理器映射器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->

<!-- 2. 该方式可替换以上配置适配器、映射器语句 -->
<mvc:annotation-driven></mvc:annotation-driven>

 

注意:用此种方法时,需要注意XML文件的头需要发生改变,改变后如下:

<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" >

5. 通过【注解】方式编写处理器

@Controller //代表该类为一个控制器/处理器
public class HelloController{

@RequestMapping("/getAllEmp")//代表该方法方法访问路径,相对于项目根
public ModelAndView getAllEmp()
{
System.out.println("HelloController.getAllEmp()....");

List<Emp> empList = new ArrayList<Emp>();
empList.add(new Emp(1000,"张三","销售"));
empList.add(new Emp(2000,"李四","职员"));
empList.add(new Emp(3000,"王五","经理"));

ModelAndView modelAndView = new ModelAndView();

modelAndView.addObject("empList", empList);

modelAndView.setViewName("main.jsp");

return modelAndView;
}

}

6. 在spring-mvc.xml文件中,配置组件扫描仪,加载所有处理器(扫描所有带类级别注解的类,例如@controller)

<!-- 处理器 -->
<!-- <bean id="helloController"class="controller.HelloController"></bean> -->

<!-- 配置组件扫描仪 :管理controller包下的所有bean,以上bean定义可省略-->
<context:component-scan base-package="controller"></context:component-scan>

7. 编写视图main.jsp
<table>
<tr>
<th>编号</th>
<th>姓名</th>
<th>职位</th>
</tr>
<c:forEach items="${empList}" var="emp">
<tr>
<td>${emp.empno}</td>
<td>${emp.ename}</td>
<td>${emp.job}</td>
</tr>
</c:forEach>
</table>

8. 在spring-mvc.xml文件中,配置视图解析器

<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

9. 测试路径

http://localhost:8088/springMvc_01_annotation/getAllEmp.action

 

posted @ 2016-12-10 14:39  龙之天族  阅读(285)  评论(0编辑  收藏  举报