SpringMVC.入门篇.一.HelloWorld

SpringMVC.入门篇《一》HelloWorld

项目包结构如下:

HelloController.java 代码

 1 package com.charles.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.ui.Model;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RequestMethod;
 7 
 8 /**
 9  * <p>Type: HelloController</p>
10  * <p>Description: Hello 控制层.</p>
11  * @author baitang.<gy03554>
12  * @date 2018年10月14日 下午3:44:57
13  * @version v1.0.0
14  */
15 @Controller
16 public class HelloController {
17 
18     @RequestMapping(value = "/hello", method = RequestMethod.GET)
19     public String hello(Model model) {
20         
21         model.addAttribute("name", "公子缘");
22         return "hello";
23     }
24 }

hello-servlet.xml 配置文件如下:

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:context="http://www.springframework.org/schema/context"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="
 5    http://www.springframework.org/schema/beans     
 6    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 7    http://www.springframework.org/schema/context 
 8    http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 9 
10     <!-- 扫描包 -->
11     <context:component-scan base-package="com.charles.controller" />
12 
13     <!-- 这个类是从 spring-webmvc 这个jar包中copy过来的。 -->
14     <bean
15         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
16         <property name="prefix" value="/" />
17         <property name="suffix" value=".jsp" />
18     </bean>
19 
20 </beans>

web.xml 配置文件如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 5     id="WebApp_ID" version="2.5">
 6 
 7     <display-name>springmvc</display-name>
 8 
 9     <welcome-file-list>
10         <welcome-file>index.jsp</welcome-file>
11     </welcome-file-list>
12 
13     <servlet>
14         <servlet-name>hello</servlet-name>
15         <!-- 前端控制器类,在spring-webmvc 这个jar包中,我是从这个包里面copy过来的。 -->
16         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
17         <load-on-startup>1</load-on-startup>
18     </servlet>
19     <servlet-mapping>
20         <servlet-name>hello</servlet-name>
21         <url-pattern>/</url-pattern>
22     </servlet-mapping>
23 
24 </web-app>

hello.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 3 <html>
 4     <head>
 5         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 6         <title>Insert title here</title>
 7     </head>
 8     
 9     <body>
10         <h2>Hello, ${name}</h2>
11     </body>
12 </html>

index.jsp 代码

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 3 <html>
 4     <head>
 5         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 6         <title>Insert title here</title>
 7     </head>
 8     
 9     <body>
10         <a href="hello">进入Hello页面.</a>
11     </body>
12 </html>

 注意:

  load-on-startup : 表示启动容器时初始化该Servlet

  url-pattern: 表示哪些请求交给Spring Web MVC处理,“/”是用来定义默认servlet映射的。也可以填写" *.html "表示拦截所有以html为扩展名的请求。

  DispatcherServlet 默认使用WebApplicationContext作为上下文,Spring默认配置文件为“/WEB-INF/[servlet 名字]-servlet.xml”

运行项目,然后打开浏览器,输入访问地址:http://localhost:9826/springmvc

点击:进入Hello页面 ,将会发起请求至后台控制层:HelloControl.java ,然后进入到:hello.jsp 页面。

如有问题,欢迎纠正!!!

如有转载,请标明源处: https://www.cnblogs.com/Charles-Yuan/p/9786676.html

原文地址:https://www.cnblogs.com/Charles-Yuan/p/9786676.html