javaEE Springmvc,入门程序,架构

Springmvc的jar包下载:https://pan.baidu.com/s/1Uu5R96z4LwwtydGq4B60Xg  密码:8c3n

Springmvc处理流程:

Springmvc架构:


src/springmvc.xml(Springmvc配置文件,其实就是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">
        
        
        <!-- 开启注解扫描。 扫描 @Controler  @Service等注解 (Springmvc的Controller是单例的,Struts2的Action是多例的)  -->
        <context:component-scan base-package="com.xxx"/>
        
        <!-- 处理器映射器(HandlerMapping) (默认的映射器DefaultAnnotationHandlerMapping已经过时) -->
        <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
        <!-- 处理器适配器(HandlAdapter) (默认的适配器AnnotationMethodHandlerAdapter已经过时) -->
        <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
        <!-- 注解驱动;可以代替上面 处理器映射器、处理器适配器的配置 -->
        <mvc:annotation-driven/>
        
        <!-- 视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        	<property name="prefix" value="/WEB-INF/jsp/"/>  <!-- 视图(jsp页面)请求路径的前缀 -->
        	<property name="suffix" value=".jsp"/>   <!-- 视图(jsp页面)请求路径的后缀 -->
        </bean>
        
   </beans>

ItemController.java(后端控制器(Handler处理器)):

package com.xxx.springmvc.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.xxx.springmvc.pojo.Items;  //实体类(商品)

//商品管理
@Controller  //通过注解声明控制器 (Spring通过注解实例化控制器)
public class ItemController {
	
	//通过注解配置拦截的URL请求
	@RequestMapping(value = "/item/itemlist.action")  // .action可以省略(省略后不利于后期维护)。
	public ModelAndView itemList(){
		
		//模拟页面需要显示的商品数据 (可通过查询数据库获得)
		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"));
		
		ModelAndView mav = new ModelAndView();
		//相当于往Request域中添加数据
		mav.addObject("itemList", list);  //jsp页面中可以通过el表达式${itemList}获取。
		
		//指定view视图(jsp页面)。
		//mav.setViewName("/WEB-INF/jsp/itemList.jsp");
		mav.setViewName("itemList");  //可以在springmvc.xml文件中配置视图解析器,配置jsp页面路径的前缀和后缀。  
		return mav;
	}
	
}

web.xml(web项目核心配置文件):

<?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>MyWeb</display-name>
  
  <!-- 前端控制器 -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 通过初始化参数,指定配置文件的位置。 默认找 /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>
  	<!-- 
  		1. /*  拦截所有,包括.jsp .js .png .css等。不推荐使用  (Struts2核心过滤器中的/*不会拦截.jsp页面等)
  		2. *.action *.do 拦截以.do .action 结尾的请求。     可以使用(一般用于后台)  
  		3. /  拦截所有 (不拦截.jsp;但拦截.js .png .css等) 推荐使用(一般用于前台、面向消费者)  (/ 需要对静态资源放行)
  	 -->
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  
  <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>
  
</web-app>

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/82794125
今日推荐