Spring MVC——搭建步驟(註解) ☆常用方式

來源:互聯網
上載者:User

標籤:ret   for   return   請求   work   2.x   依賴包   array   value   

☆常用方式

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

 

Spring MVC——搭建步驟(註解) ☆常用方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.