SpringMVC 的基本应用配置

工欲善其事,必先利其器。

软件开发中环境的熟练搭建也是一个很d疼的环节,也是无法回避的步骤。

配置的blog网上都很多,由于版本不一致,还有就是很多网页上一堆广告,感觉自己也有必要独立写一个,以备以后查阅。


1. 准备工作

。java sdk, 我用java 7

。Eclipse for Java EE 版本, 我这里用的是最新的Kepler版本

。spring framework : http://www.springsource.org/download/community, 我这里用3.1.1版本

。apache commons-logging jar包: http://commons.apache.org/proper/commons-logging/download_logging.cgi

。Apache Tomcat: 我这里用7.0.33版本


2. OK, let's go

。打开Eclipse, New -> Web -> Dynamic Web Project,下图是创建后project的目录结构:

。Project -> Properties -> Java Build Path,添加springframework 和 common-logging 2部分的external jar 包。

    把上述的这2部分jar包copy一下,粘贴到WebContent -> WEB-INF -> lib下面。

。WebContent -> WEB-INF 新建web.xml,内容如下:


<?xml version="1.0" encoding="UTF-8"?>
<web-app>
	<servlet>
	    <servlet-name>hello</servlet-name>
	    <servlet-class>
	        org.springframework.web.servlet.DispatcherServlet
	    </servlet-class>
	    <load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
	    <servlet-name>hello</servlet-name>
	    <url-pattern>/hello/*</url-pattern>
	</servlet-mapping>
	
	<welcome-file-list>
		<welcome-file>hi.html</welcome-file>
	</welcome-file-list>
</web-app>


。WebContent -> WEB-INF 新建hello-servlet.xml,取名为hello-servlet与web.xml里面的servlet-name一致,具体内容如下:

<?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:p="http://www.springframework.org/schema/p"
    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-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 
    <!-- 默认的注解映射的支持 -->
    <mvc:annotation-driven />
    <!--启用自动扫描  -->
    <context:component-scan base-package="springmvc.controller" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/html/" />
        <property name="suffix" value=".html" />
    </bean>
</beans>

ps,上述 /WEB-INF/html这个目录是自己创建的。


。在src下面创建HelloController.java

package springmvc.controller;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HelloController {

	@RequestMapping(value="/hello", method=RequestMethod.GET)
	public String sayHello(Model model) {
		model.addAttribute("msg", "Phil Jackson");
		return "hi";
	}
}

。用tomcat 启动这个web project

Right Click on the Project -> Run As ->Run on Server


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值