一、springmvc之helloworld


一、jar包下载:

查找spring framework的下载地址:

 github spring-framework 上 可以找到对应的 Access to Binaries 跳转到wiki  ,wiki中给出了Downloading a Distribution,给了包下载地址,可以在改地址中,找到所有的jar包下载路径:

https://repo.spring.io/ui/repos/tree/General/libs-release-local/org/springframework/spring;

   

二、HelloWorld项目创建步骤:

1.创建一个类型为Dynamic Web Project  名称为HelloWorld的项目;

2.加入jar包:

  • commons-logging-1.2.jar  
  • spring-beans-5.3.10.jar  
  • spring-context-5.3.10.jar  
  • spring-core-5.3.10.jar
  • spring-expression-5.3.10.jar
  • spring-web-5.3.10.jar
  • spring-webmvc-5.3.10.jar

3.配置web.xml文件

如果忘记添加的web.xml 可以通过 右击当前项目–>Java EE Tools–>Generate Deployment Descriptor 方式来生成;

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 4     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 5     version="4.0">
 6     <display-name>HelloWorlddisplay-name>
 7     
 8     <servlet>
 9         <servlet-name>springDispatcherServletservlet-name>
10         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
11         
12         
14         <init-param>
15             <param-name>contextConfigLocationparam-name>
16             <param-value>classpath:spring.xmlparam-value>
17         init-param>
18         
19         <load-on-startup>1load-on-startup>
20     servlet>
21 
22     
23     <servlet-mapping>
24         <servlet-name>springDispatcherServletservlet-name>
25         <url-pattern>/url-pattern>
26     servlet-mapping>
27 
28 
29 
30 web-app>

4.创建HelloWorld.java

 1 package handler;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 @Controller
 7 public class HelloWorld {
 8 
 9     /**
10      * 1.使用@RequestMapping 来映射请求的url
11      * 2.返回值会通过视图解析器为实现物理视图,对于nternalResourceViewResolver视图解析器
12      * 通过prefix+returnvalue+suffix 这样的方式得到实际的物理视图,然后做转发操作 /WEB-INF/VIEWS+
13      * 
14      * 
15      * 关于@RequestMapping除了修饰方法,还可用来修饰类 类定义处:提供初步的请求映射信息。相对于web应用的更目录
16      * 方法定义处:提供进一步的分映射信息。
17      * 
18      * @return
19      */
20     @RequestMapping("/helloworld")
21     public String hello() {
22         System.out.println("hello world");
23         return "success";
24     }
25 }

5.创建页面 index.jsp & success.jsp:

index.jsp:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8" isErrorPage="true"%>
 3 DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
 7 <title>Insert title heretitle>
 8 head>
 9 <body>
10 <a href="helloworld">helloa>
11 body>
12 html>

success.jsp:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="UTF-8">
 7 <title>Insert title heretitle>
 8 head>
 9 <body>
10 <h4>successh4>
11 body>
12 html>

6.创建spring.xml

spring.xml 的文件类型为 Spring bean configuration file ;

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
 7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 9 
10     <context:component-scan base-package="handler">context:component-scan>
11 
12     
13     <bean
14         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
15         <property name="prefix" value="/WEB-INF/views/">property>
16         <property name="suffix" value=".jsp">property>
17     bean>
18 
19 beans>

三、运行效果:

index页面点击 hello 跳转到 success.jsp页面 且控制台打印:hello world 

 点击 hello 跳转到 success.jsp页面;

四、目录结构: