Spring MVC学习笔记之Hello World

    xiaoxiao2022-05-21  211

    1、建立WEB项目Spring MVC

    2、引入jar包:

                commons-logging-1.1.1

                spring-aop-4.2.5.RELEASE.jar

                spring-beans-4.0.0.RELEASE.jar

                spring-context-4.0.0.RELEASE.jar

                spring-core-4.0.0.RELEASE.jar

                spring-expression-4.0.0.RELEASE.jar

                spring-web-4.0.0.RELEASE.jar

                spring-webmvc-4.0.0.RELEASE.jar

    3、配置web.xml文件,这里直接贴完整的web.xml文件,里面会有一些注释:

    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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Spring MVC</display-name> <servlet> <!-- 引入Spring MVC核心过滤器 --> <servlet-name>springDispatcherServlet</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> <!-- 启动时加载 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

    4、在src下创建springmvc.xml文件,同上,直接贴代码:

    springmvc.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置自动扫描注解的包 --> <context:component-scan base-package="com.watimer.springmvc"></context:component-scan> <!-- 配置视图解析器 --> <!-- 视图解析器的作用为:当请求成功时,跳转到“视图解析器前缀+返回值+视图解析器后缀界面” 以此项目为例,则跳转到WEB-INF/views/success.jsp界面 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>

    5、在SRC下建包,注意与springmvc.xml中配置的要相同,这里为com.watimer.springmvc,同时建立类Hello,贴代码:

      Hello.java

    package com.watimer.springmvc; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * @author 易讯.岳耀栎 */ //扫描Controller @Controller public class Hello { //@RequestMapping对应的是请求路径,会在下文中指出 @RequestMapping("/hello") public String hello(){ System.out.println("hello world"); //请求成功后,根据上文配置的视图解析器,会跳转到success.jsp界面 return "success"; } }

    6、在WEB-INF目录下,建立views文件夹,同时在views文件夹内新建success.jsp,一会会贴出项目的截图:

    7、在index添加请求超链接,点击时会打印输出hello world,同时跳转到success.jsp界面:

       index.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <!-- 这里的请求,对应的是@RequestMapping("/hello"),如果不一致,则请求不到--> <a href="hello">helloworld</a> </body> </html>

    7、请求界面

    8、点击后的界面

    8、控制台的输出

    到此,第一个项目就完成了,项目源码可供下载


    最新回复(0)