天天看點

springmvc學習整理(一) springmvc的基本配置

工作時間也不短了,一直沒有沉下心研究技術。真的很慚愧。

springmvc+spring+mybatis基本上算是電商的标配了。話不多說今天自己整理學習一下Springmvc

首先,springmvc就是spring中一個子產品,與spring無縫整合。

接着,看看springmvc的運作原理。

springmvc學習整理(一) springmvc的基本配置

1.導入jar包 

springmvc學習整理(一) springmvc的基本配置

2.springmvc的入口是servlet是以先配置web.xml的servlet

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID" version="2.5">

  <display-name>my_spring_mvc</display-name>

  <!-- 前端控制器的配置 -->

  <servlet>

   <servlet-name>my_spring_mvc</servlet-name>

   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

   <!-- 指定加載springmvc前端控制器加載所需的檔案 -->

   <init-param>

   <param-name>contextConfigLocation</param-name>

   <param-value>classpath:spring/springmvc.xml</param-value>

   </init-param>

   <!-- 啟動servlet -->

   <load-on-startup>1</load-on-startup>

  </servlet>

  <!-- 配置攔截的url -->

  <servlet-mapping>

   <servlet-name>my_spring_mvc</servlet-name>

   <!--

   /:springmvc RESTful風格

   /*:配置不行 之前在struts2中配置/*這裡是不可以的,會把jsp頁面也會攔截

   *.action:攔截所有action結尾的請求

    -->

   <url-pattern>*.action</url-pattern>

  </servlet-mapping>

  <!-- 亂碼過慮器 -->

<filter>

<filter-name>CharacterEncodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>utf-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>CharacterEncodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

</web-app>

3.開發中一般使用注解方式實作controller

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.1.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">

<!-- 視圖解析器 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF"/> 簡化傳回視圖的路徑

<property name="suffix" value=".jsp"/>

</bean>

<!-- 基于注解 -->

<!-- 掃描controller -->

<context:component-scan base-package="cn.baidu.test01"/>

<!-- 注解處理器映射器的配置 -->

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

<!-- 注解處理器擴充卡的配置 -->

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

</beans>

4.編寫controller

@Controller  //基于注解聲明這是一個controller

@RequestMapping("/item") //窄化請求 可以參照struts2了解

public class TestAction {

@RequestMapping("/getModel.action")

public ModelAndView getModel(){

ModelAndView mv = new ModelAndView("/item/index");

return mv;

}

@RequestMapping("/getStudent.action") //配置通路的url 實際上傳回string類型與放回modelandview類型效果是一樣的也不存在效率的問題使用的話看自己的喜好吧!

public ModelAndView getStudent(){

ModelAndView mv = new ModelAndView("/item/mytest");

List<ProductDTO> list = new ArrayList<>();

ProductDTO  pro1 = new ProductDTO();

pro1.setPrice("211");

pro1.setNum("1");

ProductDTO  pro2 = new ProductDTO();

pro2.setPrice("212");

pro2.setNum("2");

ProductDTO  pro3 = new ProductDTO();

pro3.setPrice("213");

pro3.setNum("3");

ProductDTO  pro4 = new ProductDTO();

pro4.setPrice("214");

pro4.setNum("4");

list.add(pro1);

list.add(pro2);

list.add(pro3);

list.add(pro4);

Student s1 = new Student();

s1.setName("李秀偉大笨蛋");

s1.setAge(16);

mv.addObject("student", s1);

mv.addObject("proList", list);

return mv;

}

}

 運作以後的結果。

springmvc學習整理(一) springmvc的基本配置
springmvc學習整理(一) springmvc的基本配置

再次,希望自己能夠堅持去學習了解技術。

springmvc學習整理(一) springmvc的基本配置

!如有不正确的地方希望大家指正。共同進步!