springmvc-helloworld

1.什么是springmvc?

Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的SpringMVC框架或集成其他MVC开发框架,如Struts1,Struts2等。

写一个HelloWorld示例

软件参数:

Eclipse: Mars Milestone 2 (4.5.0M2)

jdk:1.7

tomcat:7

spring:4

相关jar包下载:链接: https://pan.baidu.com/s/1mip9gBQ 密码: nfna

1.新建项目

File-New-Other,选择Dynamic web project

2.导入jar包

3.配置文件及编写代码

  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_3_0.xsd"
 5     id="WebApp_ID" version="3.0">
 6     <welcome-file-list>
 7         <welcome-file>index.jsp</welcome-file>
 8     </welcome-file-list>
 9     <!-- 配置Dispatcher servlet -->
10     <servlet>
11         <servlet-name>springDispatcherServlet</servlet-name>
12         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
13         <!-- 配置Spring mvc下的配置文件的位置和名称 -->
14         <init-param>
15             <param-name>contextConfigLocation</param-name>
16             <param-value>classpath*:springmvc.xml</param-value>
17         </init-param>
18         <load-on-startup>1</load-on-startup>
19     </servlet>
20     <servlet-mapping>
21         <servlet-name>springDispatcherServlet</servlet-name>
22         <url-pattern>/</url-pattern>
23     </servlet-mapping>
24 </web-app>

注意:  14~17配置springmvc.xml的位置

     20~23表示对所有路径都进行过滤

springmvc.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 6     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
10         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
11 
12     <!-- 配置自动扫描的包 -->
13     <context:component-scan base-package="com.jackie.springmvc"></context:component-scan>
14 
15     <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
16     <bean
17         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
18         <property name="prefix" value="/WEB-INF/views/"></property>
19         <property name="suffix" value=".jsp"></property>
20     </bean>
21 </beans>

helloworld.java

 1 package com.jackie.springmvc;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 @Controller
 7 public class HelloWord {
 8     /**
 9      * 1. 使用RequestMapping注解来映射请求的URL 
10      * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于InternalResourceViewResolver视图解析器,会做如下解析 通过prefix+returnVal+suffix
11      *         这样的方式得到实际的物理视图,然后会转发操作 "/WEB-INF/views/success.jsp"
12      * 
13      * @return
14      */
15     @RequestMapping("/helloworld")
16     public String hello() {
17         System.out.println("hello world");
18         return "success";
19     }
20 }

@Controller 表示是spring的控制器

@RequestMapping("/helloworld") 用于匹配请求的路径

return “success” 与springmvc.xml配合,表示返回success.jsp

下面贴出index.jsp和success.jsp代码

index.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 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 world</a>
11 </body>
12 </html>

success.jsp

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 
11     <h4>Success Page</h4>
12 
13 </body>
14 </html>
岁月本长而忙者自促;天地本宽而卑者自隘;风花雪月本闲,而劳忧者自冗;天行健,君子以自强不息;地势坤,君子以厚德载物;宠辱不惊,闲看庭前花开花落;去留无意,漫随天外云卷云舒.不妄取,不妄予,不妄想,不妄求,与人方便,随遇而安
原文地址:https://www.cnblogs.com/vvning/p/7573814.html