欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springmvc入门案例

程序员文章站 2022-07-04 19:23:22
...
  1. 文件的项目结构
    springmvc入门案例
  2. 创建web项目配置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>SpringmvcDemo</display-name>
  
  <!-- 配置前端控制器  类似于servlet-->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- contextConfigLocation配置 springmvc加载的配置文件(配置处理器轻奢器    适配器等等)
  		如果不配置 contextConfigLocation  默认加载的是/WEB?INF/servlet名称-servlet.xml(springmvc-servlet。xml)
  	-->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  		<servlet-name>springmvc</servlet-name>
  		<!-- 第一种 *.action  访问以action结尾  由Dispatcherservlet进行解析
  			第二种  /  所有访问的地址都由DispatcherServlet进行解析  对于静态的文件解析  需要配置不让DispatcherServlet进行解析
  			使用此种方式可以实现RESTful风格的url
  			第三种  /*  这样配置不对 
  		  -->
  		<url-pattern>*.action</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
  1. 配置springmvc.xml文件
    主要有以下的配置
    handler
    <bean name="/queryHttpItem.action" class="com.sofency.top.controller.ItemHttpController"></bean>
    处理器映射器(两种)
    • <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
    • 简单的url处理器映射器 需要将handler设置id
      <bean id="itemController" name="/queryItem.action" class="com.sofency.top.controller.ItemController"></bean>
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
         	<props>
         	<--	可以通过下面两个链接都可以进行访问-->
         			<prop key="/queryItem1.action">itemController</prop>
         			<prop key="/queryItem2.action">itemController</prop>
         		</props>
         	</property>
     </bean>
    

处理器适配器

  • 1 返回值为modelandview
 <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
  • 返回值为void
 <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>

视图解析器

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

综合而得的springmvc.xml 第一种写法

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        <!-- 创建Handler 即controller -->
        <bean name="/queryHttpItem.action" class="com.sofency.top.controller.ItemHttpController"></bean>
        <!--处理器映射器-->
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
        <!-- 处理器适配器 
        所有的处理器适配器都实现了HandlerAdapter的接口   要求实现controller接口-->
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
        <!--视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>

第一种写法所对应的handler的写法

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.sofency.top.pojo.Item;

public class ItemController implements Controller {
	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//调用service操作数据库  查询商品列表 这里使用的是静态数据进行模拟
		List<Item> itemList = new ArrayList<Item>();
		Item item1 = new Item();
		item1.setItemid(1);
		item1.setItemname("sofency");
		item1.setItemprice(12);
		
		Item item2 = new Item();
		item2.setItemid(2);
		item2.setItemname("alice");
		item2.setItemprice(10);
		itemList.add(item1);
		itemList.add(item2);
		
		//返回modelAndView
		ModelAndView model = new ModelAndView();
		model.addObject("itemList", itemList);
		model.setViewName("/WEB-INF/jsp/items/itemList.jsp");
		return model;
	}
}

springmvc.xml的第二种写法

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        <!-- 创建Handler -->
        <bean id="itemController" name="/queryItem.action" class="com.sofency.top.controller.ItemController"></bean>
       <!--处理器映射器-->
       <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
         		<property name="mappings">
         			<props>
         			<!--可以通过下面两个链接都可以进行访问-->
         				<prop key="/queryItem1.action">itemController</prop>
         				<prop key="/queryItem2.action">itemController</prop>
         			</props>
         		</property>
         </bean> 
        <!-- 处理器适配器 -->
         <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
        <!-- 视图解析器     解析jsp 默认使用jstl标签  classpath下需要有jstl的包 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>   
</beans>

注:上面的处理器映射器可以混合使用
第二种所对应的handler的写法

package com.sofency.top.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.HttpRequestHandler;
import com.sofency.top.pojo.Item;
public class ItemHttpController implements HttpRequestHandler{

	@Override
	public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<Item> itemList = new ArrayList<Item>();
		Item item1 = new Item();
		item1.setItemid(1);
		item1.setItemname("sofency");
		item1.setItemprice(12);
		
		Item item2 = new Item();
		item2.setItemid(2);
		item2.setItemname("alice");
		item2.setItemprice(10);
		itemList.add(item1);
		itemList.add(item2);
		
		//类似于servlet的实现
		//设置模型数据
		request.setAttribute("itemList", itemList);
		//设置转发的视图
		request.getRequestDispatcher("/WEB-INF/jsp/items/itemList.jsp").forward(request, response);
	}
}

springmvc.xml的第三种写法 注解的方式

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

         <!-- 扫描handler  扫描所有的controller的方式 -->
         <context:component-scan base-package="com.sofency.top.controller"></context:component-scan>
         
		<!-- 配置注解类的映射器和适配器 -->
        <!-- 注解类的映射器 -->
        <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  代理上面两个映射器和适配器配置
       		使用mvc:anotation-driven 默认加载很多的参数绑定方法
       		实际开发中使用mvc:annotation-driven
       	 -->
       	<mvc:annotation-driven></mvc:annotation-driven>
        <!-- 视图解析器     解析jsp 默认使用jstl标签  classpath下需要有jstl的包 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        	<!-- 配置视图解析器的前缀和后缀 -->
        	<property name="prefix" value="/WEB-INF/jsp/"></property>
        	<property name="suffix" value=".jsp"></property>
        </bean>
</beans>

注解所对应的handler

package com.sofency.top.controller;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.sofency.top.pojo.Item;
@Controller
public class ItemAnotationController {
	
	@RequestMapping("/queryItems")
	public ModelAndView queryItems() throws Exception {
		List<Item> itemList = new ArrayList<Item>();
		Item item1 = new Item();
		item1.setItemid(1);
		item1.setItemname("sofency");
		item1.setItemprice(12);
		
		Item item2 = new Item();
		item2.setItemid(2);
		item2.setItemname("alice");
		item2.setItemprice(10);
		itemList.add(item1);
		itemList.add(item2);
		//返回modelAndView
		ModelAndView model = new ModelAndView();
		model.addObject("itemList", itemList);
		//前缀和后缀在视图解析器中配置
		model.setViewName("items/itemList");	
		return model;
	}	
}
相关标签: springmvc