您的位置:首页 > 编程语言 > Java开发

一步一步学习springmvc之一:基本环境的搭建

2013-05-13 13:32 239 查看
1、在eclipse中创建一个动态的webapp

2、WEB-INF/lib导入需要的jar文件

spring.jar (from spring-framework-2.5/dist)

spring-webmvc.jar(from spring-framework-2.5/dist/modules)

commons-logging.jar (from spring-framework-2.5/lib/jakarta-commons)

3、配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- 映射的配置 -->
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 將所有以*.htm結尾的请求交给 org.springframework.web.servlet.DispatcherServlet-->
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>

</web-app>


4.springapp-servlet.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
<!-- 配置DispatcherServlet -->
<!-- /hello.htm的请求交给 springapp.web.HelloController-->
<bean name="/hello.htm" class="springapp.web.HelloController"/>
</beans>


5、创建一个控制器

package springapp.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new ModelAndView("hello.jsp");
}
}


6、然后在WebContent目录下创建一个hello.jsp

<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<!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=gbk">
<title>Insert title here</title>
</head>
<body>
<h1>Hello - Spring Application</h1>
<p>Greetings.</p>
</body>
</html>


6、启动tomcat在浏览器中输入:http://localhost:8080/springmvc/hello.htm即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: