标签:context tar -- code 目录 pat 位置 分享 4.0
准备条件:
STS(集成了Spring相关工具的Eclipse)
步骤:
Eclipse中新建一个动态的web工程。选择Tomcat 7.0,在WebContent-->WEB-INF-->lib目录下添加以下jar包。
spring-aop-4.3.3.RELEASE.jar
spring-beans-4.3.3.RELEASE.jar
spring-context-4.3.3.RELEASE.jar
spring-core-4.3.3.RELEASE.jar
pring-expression-4.3.3.RELEASE.jar
spring-web-4.3.3.RELEASE.jar
spring-webmvc-4.3.3.RELEASE.jar
另外还需要commons-logging-1.2.jar文件。
2. 在web.xml中添加dispatcherServlet相关内容(使用快捷键Alt+/,在自动弹出的下拉条中,选择#dispatcherservlet)。web.xml配置文件内容可以参考如下内容:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>HelloWorld</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置dispathcerservlet的一个初始化参数,设置配置文件的名称和位置 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3. 加入SpringMVC的配置文件。在src目录下,参照以下关键步骤创建springmvc.xml文件。
参考如下代码:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!-- 配置自动扫描包 --> <context:component-scan base-package="com.tiekui.springmvc.*"></context:component-scan> <!-- 配置视图解析器:如何把handler方法返回给视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
4.编写请求的处理器。参考如下代码:
package com.tiekui.springmvc.handlers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorld { @RequestMapping("/helloworld") public String hello(){ System.out.println("hello world"); return "success"; } }
5. 编写视图。在WebContent目录下创建index.jsp文件,参考如下代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="helloworld">Hello World</a>
</body>
</html>
在/WEB-INF/views目录下创建success.jsp。内容参考如下代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello SpringMVC View</h1>
</body>
</html>
标签:context tar -- code 目录 pat 位置 分享 4.0
原文地址:http://www.cnblogs.com/zhoutiekui/p/6220783.html