springMVC的搭建环境
一、搭建框架,首先新建一个项目工程
直接下一步,完成即可。
二、将必需的jar包导入到项目中
三、然后配置web.xml
xmlns=”http://java.sun.com/xml/ns/javaee”
xmlns:web=”http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”
id=”WebApp_ID” version=”2.5″>
Spring3MVC
index.jsp
spring
org.springframework.web.servlet.DispatcherServlet
1
spring
/
四、配置spring-servlet.xml
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd”>
–>
base-package=”net.viralpatel.spring3.controller” />
class=”org.springframework.web.servlet.view.UrlBasedViewResolver”>
value=”org.springframework.web.servlet.view.JstlView” />
五、建立jsp界面
index.jsp和jsp/hello.jsp
index.jsp界面:
Spring 3.0 MVC Series: Index – ViralPatel.netjsp/hello.jsp
Spring 3.0 MVC Series: Hello World – ViralPatel.net${message}
六、建立controller代码
HelloWorldController.java代码
package net.viralpatel.spring3.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping(“/hello”)
public ModelAndView helloWorld() {
String message = “Hello World, Spring 3.0!”;
System.out.println(message);
return new ModelAndView(“hello”, “message”, message);
}
}
工程的结构图如下:
运行即可。
我这里用的是tomcat8.0.28,jdk是1.8,这样建立之后,默认编译器版本为1.8,但是该项目只能用1.7版本的编译,否则出现以下错误:
将编译版本调成1.7即可运行,如下图:
修改后,不影响使用,但是在项目上面会出现红色叉叉,在项目文件的.settings文件夹中的org.eclipse.wst.common.project.facet.core.xml,修改该文件中的编译版本为你修改的值即可
终于测试完一个项目了
参考资料:http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/
附件中上传的源码Spring3MVC-part1.rar