SpringMVC笔记

4 篇文章 0 订阅

SpringMVC为WEB项目表现层框架,与Struts2和初始的servlet技术作用相同,ssm:SpringMVC,Spring,MyBatis。

架构:

三大组件:

HandlerMapping:处理请求路径,映射到controller的方法,返回Handler:包名、类名、方法名。(@RequestMapping)

HandlerAdapter:执行controller的方法(handler),参数绑定。

ViewResolver:解析ModelAndView的视图部分。

 

开发部分:Handler(controller),jsp

 

开发流程

项目结构:

1、创建springmvc.xml配置文件

<?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:p="http://www.springframework.org/schema/p"
	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-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

        <!-- 处理器映射器 -->
	<!--  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>-->
	<!-- 处理器适配器 -->
	<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>-->
	<!-- 二合一 -->
	<mvc:annotation-driven/>
	
	<!-- 视图解析器(可以不配) -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- ModelAndView中viewname前缀 -->
		<property name="prefix" value="/WEB-INF/"/>
		<!-- 后缀 -->
		<!-- <property name="suffix" value=".jsp" /> -->
	<!-- 配置controller扫描包 -->
	<context:component-scan base-package="com.example.controller" />

</beans>

2、导入jar包

3、配置核心servlet(前端控制器)

web.xml:

<servlet>
	<servlet-name>SpringMVC</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:springmvc.xml</param-value>
	</init-param>
</servlet>
<servlet-mapping>
  	<servlet-name>SpringMVC</servlet-name>
  	<url-pattern>*.action</url-pattern>
</servlet-mapping>

4、创建controller类

package com.example.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.example.pojo.Items;

@Controller
public class ItemController {

	@RequestMapping(value="/item/itemlist.action")
	public ModelAndView itemList() {
		ModelAndView mav = new ModelAndView();
		List<Items> list = new ArrayList<Items>();
		list.add(new Items(1, "1华为 荣耀8", 2399f, new Date(), "质量好!1"));
		list.add(new Items(2, "2华为 荣耀8", 2399f, new Date(), "质量好!2"));
		list.add(new Items(3, "3华为 荣耀8", 2399f, new Date(), "质量好!3"));
		list.add(new Items(4, "4华为 荣耀8", 2399f, new Date(), "质量好!4"));
		list.add(new Items(5, "5华为 荣耀8", 2399f, new Date(), "质量好!5"));
		list.add(new Items(6, "6华为 荣耀8", 2399f, new Date(), "质量好!6"));
		mav.addObject("itemList", list);
		mav.setViewName("jsp/itemList.jsp");
		return mav;
	}
}

 

访问Project/item/item.action

 

SSM整合

项目结构:

Spring配置文件:applicationContext.xml(主要为Mybatis的配置)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<context:property-placeholder location="classpath:db.properties"/>
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	<!-- mybatis的工厂 -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
	</bean>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 自动找sqlSessionFactoryBean (不需要id)-->
		<property name="basePackage" value="com.example.mapper"/>
	</bean>
	
	<bean class="com.example.excepion.ItemExceptionResolver"/>
	
	<!-- 注解事务 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 开启注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

 

springmvc.xml(主要扫描controller及service类进Spring容器)

<?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:p="http://www.springframework.org/schema/p"
	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-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<!-- 配置controller扫描包@Controller @Service -->
	<context:component-scan base-package="com.example" />

	<!-- 处理器映射器 -->
	<!--  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>-->
	<!-- 处理器适配器 -->
	<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>-->
	<mvc:annotation-driven conversion-service="conversionbean"/>
	
	<!-- 自定义参数类型绑定 -->
	<bean id="conversionbean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<property name="converters">
			<array>
				<bean class="com.example.utils.DateFormatConverter"></bean>
			</array>
		</property>
	</bean>
	
	<!-- 视图解析器(可以不配) -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/"></property>
	</bean>
</beans>

 

sqlmap.xml(指定mapper中默认类名的包名)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<typeAliases>
		<package name="com.example.pojo"/>
	</typeAliases>
</configuration>

 

web.xml

<?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" 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>SpringMVC_mybatis</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- spring监听器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- post提交乱码过滤器 -->
  <filter>
  	<filter-name>EncodeFilter</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>EncodeFilter</filter-name>
  	<url-pattern>*.action</url-pattern>
  </filter-mapping>
  
  <!-- 前端控制器 -->
  <servlet>
	<servlet-name>SpringMVC</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:springmvc.xml</param-value>
	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>SpringMVC</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

 


2019.03.02

使用参数匹配时,handler中所有参数中与前台name值匹配都会被赋值,包括pojo与简单实体类型同时存在,而如果pojo属性或参数中存在与前台name匹配的input值类型不匹配时,前台报400错误,不管是否另有其他值与前台类型匹配。最常见的是Date类型,需要配置转换器。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值