zoukankan      html  css  js  c++  java
  • springMVC实现HelloWorld

    我行过许多地方的桥,看过许多次数的云,喝过许多种类的酒,却始终没见过最适合初学者的HelloWorld。

    项目截图(jar包自行添加)

    1、前端控制器的配置(在web.xml中配置)

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    	<!-- 配置前端控制器 -->
    	<servlet>
    		<servlet-name>springmvc</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>springmvc</servlet-name>
    		<!-- 拦截请求 -->
    		<!-- 拦截固定后缀的请求,可以为*.do,*.action等
    		     拦截所有,设置为/
    		     拦截所有时不能设置为/* 因为请求到Action,当action转到jsp时再次被拦截 -->
    		<url-pattern>*.do</url-pattern>
    
    	</servlet-mapping>
    </web-app>
    

    2、DispatcherServlet默认会注册HandlerMapping和HandlerAdapter

    3、在Spring配置文件中配置ViewResolver,Spring配置文件在没有配置<servlet><init-param>属性时,默认为/WEB-INF目录下的[servlet-name]-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context"
    	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-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    	<!-- 配置视图解析器 -->
    	<!-- InternalResourceViewResolver:支持JSP视图解析
    	     viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,classpath中必须包含jstl的相关jar包
    	     prefix 和suffix:查找视图页面的前缀和后缀
    	     最终视图的址为:前缀+逻辑视图名+后缀
    	     逻辑视图名需要在controller中返回ModelAndView指定
    	     假如逻辑视图名为hello,则最终返回的jsp视图地址为"WEB-INF/jsp/hello.jsp" -->
    	<bean
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="viewClass"
    			value="org.springframework.web.servlet.view.JstlView" />
    		<property name="prefix" value="/WEB-INF/jsp/" />
    		<property name="suffix" value=".jsp" />
    	</bean>
    </beans>
    

    4、开发处理器/页面控制器(实现Controller接口)

    package com.zj.springmvc;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;
    
    public class HelloWorld implements Controller {
    
    	@Override
    	public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
    		ModelAndView mv = new ModelAndView();
    		mv.addObject("message", "hello world!"); // 类似request.setAttribute("","")
    		mv.setViewName("hello"); // 类似request.getRequestDispatcher("url").forward(request, response);
    		return mv;
    
    	}
    
    }
    

    5、将其添加到Spring配置文件(上文中的springmvc-servlet.xml)中,让其接受SpringIOC容器管理

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context"
    	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-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    	<!-- 配置视图解析器 -->
    	<!-- InternalResourceViewResolver:支持JSP视图解析
    	     viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,classpath中必须包含jstl的相关jar包
    	     prefix 和suffix:查找视图页面的前缀和后缀
    	     最终视图的址为:前缀+逻辑视图名+后缀
    	     逻辑视图名需要在controller中返回ModelAndView指定
    	     假如逻辑视图名为hello,则最终返回的jsp视图地址为"WEB-INF/jsp/hello.jsp" -->
    	<bean
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="viewClass"
    			value="org.springframework.web.servlet.view.JstlView" />
    		<property name="prefix" value="/WEB-INF/jsp/" />
    		<property name="suffix" value=".jsp" />
    	</bean>
    
    	<bean name="/hello.do" class="com.zj.springmvc.HelloWorld" />
    </beans>
    

    6、开发视图页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>${message }
    </body>
    </html>
    

  • 相关阅读:
    在安装SqlServer2008时,有一项安装程序支持规则,为什么重新启动计算机那一项总是失败
    在 ServiceModel 客户端配置部分中,找不到引用协定“WebServiceTest.WebServiceSoap”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素
    IIS7上设置MIME让其支持android和Iphone的更新下载
    c# 常用正则
    数据库增容方法
    初识MAC(由window到mac的转变适应)
    无需控件直接导出xls(csv)
    2017中国屏幕分辨率统计---UI设计应注重的问题
    网页制作基础及HTML教学模块安排
    传统教学设计模板
  • 原文地址:https://www.cnblogs.com/zj0511/p/7203555.html
Copyright © 2011-2022 走看看