ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

一、springmvc之helloworld

2022-03-08 13:03:05  阅读:175  来源: 互联网

标签:xml 10 success web springmvc spring jar 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>HelloWorld</display-name>
 7     <!-- 配置DispatcherServlet(快捷键 alt +/) -->
 8     <servlet>
 9         <servlet-name>springDispatcherServlet</servlet-name>
10         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
11         <!-- 配置DispatcherServletd 一个初始化参数:配置springmvc配置文件的位置和名称 -->
12         <!-- 实际上也可以不通过 contextConfigLocation 来配置Springmvc的配置文件,而是用默认的 即默认的配置文件为 
13             /WEB-INF/<servlet-name>-servlet.xml 本项目默认位置配置文件即为: /WEB-INF/springDispatcherServlet-servlet.xml -->
14         <init-param>
15             <param-name>contextConfigLocation</param-name>
16             <param-value>classpath:spring.xml</param-value>
17         </init-param>
18         <!-- 表示springDispatcherServlet在加载的时候被创建 -->
19         <load-on-startup>1</load-on-startup>
20     </servlet>
21 
22     <!-- Map all requests to the DispatcherServlet for handling -->
23     <servlet-mapping>
24         <servlet-name>springDispatcherServlet</servlet-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 here</title>
 8 </head>
 9 <body>
10 <a href="helloworld">hello</a>
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 here</title>
 8 </head>
 9 <body>
10 <h4>success</h4>
11 </body>
12 </html>
View Code

 

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     <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
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页面;

 

 

 

四、目录结构:

 

标签:xml,10,success,web,springmvc,spring,jar,helloworld
来源: https://www.cnblogs.com/lixiuming521125/p/15979665.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有