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

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即可


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值