springMVC它在框架设计性、扩展性、灵活性等方面全面超越Struts、WebWork等MVC框架,从原来的追赶者一跃成为MVC的领跑者。我们今天介绍一下SpringMVC的入门。
首先,我们导入SpringMVC的jar架包
项目完整结构(注意:这里的out文件夹不用创建,运行项目时会自动生成)
接下来配置web.xml,代码如下
1<?xml version="1.0" encoding="UTF-8"?>
2<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 5http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
6 version="4.0">
7
8 <!-- The front controller of this Spring Web application, responsible for 9 handing all application requests -->
10 <!--
11 DispatcherServlet的配置用插件来完成。
12 注意事项:
13 Ⅰ、初始化参数:告知当前springmvc的配置文件路径。
14 <init-param>
15 <param-name>contextConfigLocation</param-name>
16 <param-value>classpath:springmvc.xml</param-value>
17 </init-param>
18 Ⅱ、配置当前servlet映射,url-pattern修改为/
19 -->
20
21 <servlet>
22 <servlet-name>dispatcherServlet</servlet-name>
23 <servlet-24class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
25 <init-param>
26 <param-name>contextConfigLocation</param-name>
27 <param-value>classpath:springmvc.xml</param-value>
28 </init-param>
29 <load-on-startup>1</load-on-startup>
30 </servlet>
31
32 <!-- Map all request to the DispatcherServlet for hanging-->
33 <servlet-mapping>
34 <servlet-name>dispatcherServlet</servlet-name>
35 <url-pattern>/</url-pattern>
36 </servlet-mapping>
37</web-app>
下面配置springmvc.xml,代码如下
1<?xml version="1.0" encoding="UTF-8" ?> <beans
2 xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
5 xmlns:context="http://www.springframework.org/schema/context"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
8 http://www.springframework.org/schema/context
9 http://www.springframework.org/schema/context/spring-context-4.0.xsd">
10
11 <!--配置
12 @Controller
13 @Service
14 -->
15 <context:component-scan base-package="com.springmvc.handler"/>
16
17 <!--视图解析器:逻辑视图,物理视图-->
18 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
19 <property name="prefix" value="/"/>
20 <property name="suffix" value=".jsp"/>
21 </bean>
22</beans>
到此为止,我们完成了配置文件,接下来我们开始编写java代码。
首先,我们创建一个 com.springmvc.handler 的包,代码如下
1 package com.springmvc.handler;
2 import org.springframework.stereotype.Controller;
3 import org.springframework.web.bind.annotation.RequestMapping;
4
5 @Controller
6 public class HelloWorldHandler {
7
8 /**
9 * 控制器处理请求的业务方法格式:
10 * public String 方法名称(){
11 *
12 * }
13 * String:当前方法处理完毕之后,所要返回的逻辑视图名称。
14 */
15 @RequestMapping(value = "/hello")
16 public String hello(){
17 System.out.println("进入了hello方法中....");
18 return "success";
19 }
20 }
根据返回的逻辑视图名称,编写success.jsp,此次,我们只做简单的输出,代码如下
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
2 <html>
3 <head>
4 <title>springmvc</title>
5 </head>
6 <body>
7 Success Page.....
8 </body>
9 </html>
另外,需要编写一个jsp页来访问,代码如下
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
2 <html>
3 <head>
4 <title>springmvc</title>
5
6 </head>
7
8 <body>
9 <a href="hello">Hello World</a>
10 </body>
11 </html>
自此,一个简单的springmvc项目完成,项目中可能有不足,如有错误,请指出!!!