搭建spring3 mvc框架
一、下载相关的包,这个就不在赘述
二、用Eclipse建立一个项目,怎么建立项目也不在多说,然后把包加入到lib下
org.springframework.web.servlet-3.0.1.RELEASE-A.jar
org.springframework.web-3.0.1.RELEASE-A.jar
org.springframework.core-3.0.1.RELEASE-A.jar
org.springframework.context-3.0.1.RELEASE-A.jar
org.springframework.beans-3.0.1.RELEASE-A.jar
org.springframework.asm-3.0.1.RELEASE-A.jar
org.springframework.expression-3.0.1.RELEASE-A.jar
org.springframework.context.support-3.0.1.RELEASE-A.jar
asm-2.2.3.jar
commons-fileupload.jar
commons-logging.jar
jstl.jar 复制代码
三、配置web.xml
spring_mvc
g3top
org.springframework.web.servlet.DispatcherServlet-------①
contextConfigLocation-----②
/WEB-INF/classes/com/g3top/config/spring_*.xml -----③
1
g3top
*.do
index.jsp
复制代码
① 这个地方是配置spring mvc的拦截地方,因为spring mvc是机遇Dispatcher的机制来转发request的,这个地方就是设置类; ② 这个地方是配置对应的spring mvc的配置文件; ③ 这设置配置文件的位置,当然,这个和2都可以不设置,使用默认的,但是,一般是很少这么使用的,所以,这里就是弄成了自定义的,方便大家。
三、配置spring_mvc.xml 这个配置文件,就是在上面的3的那个位置的文件
<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"
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">
<context:component-scan base-package="com.g3top.*"/> -----①
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
复制代码
①这个地方是是扫描包的地方,至于这个的用法,以后在介绍,这次只是让大家知道如何架构最简单的mvc框架
四、对应的java文件
package com.g3top.control;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldControl {
@RequestMapping("/hello")
public ModelAndView helloWorld() {
ModelAndView mav = new ModelAndView();
mav.setViewName("helloWorld");
mav.addObject("message", "Hello World!");
return mav;
}
} 复制代码
五、对应的jsp文件
在更目录下建立一个jsp文件夹然后建立helloWorld.jsp文件
就这样,就可以直接跑了。