• 第一个springMVC入门程序


    先看下项目结构

    要加载与spring相关的包

    HelloController.java(注意是在包controller下)

    package controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    
    @Controller
    public class HelloController {
    
    	@RequestMapping(value="/welcome",method = RequestMethod.GET)
    	public String printWelcome(ModelMap model) {
    
    		model.addAttribute("message", "Spring 3 MVC Hello World");
    		return "hello";
    
    	}
    	
    }
    

    hello.jsp(注意是放在WEB-INF下的pages目录下) 

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!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=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    	<h1>Message : ${message}</h1>	
    </body>
    </html>
    

    mvc-dispatcher-xml (注意:一般是放置在WEB-INF目录下,可以放置在src目录下) 

    <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">
     
    	<context:component-scan base-package="controller" />
     
    	<bean
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix">
    			<value>/WEB-INF/pages/</value>
    		</property>
    		<property name="suffix">
    			<value>.jsp</value>
    		</property>
    	</bean>
     
    </beans>
    

    web.xml放置在WEB-INF 目录下)

    <web-app id="WebApp_ID" 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">
     
    	<display-name>Spring MVC Application</display-name>
     
    	<servlet>
    		<servlet-name>mvc-dispatcher</servlet-name>
    		<servlet-class>
                           org.springframework.web.servlet.DispatcherServlet
                    </servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
     
    	<servlet-mapping>
    		<servlet-name>mvc-dispatcher</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
     
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    	</context-param>
     
    	<listener>
    		<listener-class>
                          org.springframework.web.context.ContextLoaderListener
                    </listener-class>
    	</listener>
     
    </web-app>
    

    index.jsp

    ============这个可以里面什么都不用写==========最后启动tomcat服务器,在url中输入 localhost:8080/spring/welcome

    结果

     

  • 相关阅读:
    团队工作第四次推进之——软件设计规格说明书
    失物找寻APP软件需求规格说明书——第三次团队作业
    你还在为校园内丢失东西无处可寻而发愁吗?速戳进来
    十分有趣却有些遗憾的结对编程——两位女程序员的挣扎
    结对编程初涉猎——结对伙伴的代码复审
    个人实战演练全过程——No.1 最大连续子数组求和
    小白出品 单元测试相关——入门级说明书
    写着写着停不下来的普通女程序员的总结
    vs2010 和vs2012的区别 副标题--Loaded事件走两次
    汽车防撞软件引发的一套软件系统思路
  • 原文地址:https://www.cnblogs.com/JAYIT/p/4125277.html
Copyright © 2020-2023  润新知