工作时间也不短了,一直没有沉下心研究技术。真的很惭愧。
springmvc+spring+mybatis基本上算是电商的标配了。话不多说今天自己整理学习一下Springmvc
首先,springmvc就是spring中一个模块,与spring无缝整合。
接着,看看springmvc的运行原理。
1.导入jar包
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;
}
}
运行以后的结果。
再次,希望自己能够坚持去学习理解技术。!如有不正确的地方希望大家指正。共同进步!