当前位置: 代码迷 >> VC/MFC >> eclipse 搭建SpringMVC框架1
  详细解决方案

eclipse 搭建SpringMVC框架1

热度:114   发布时间:2016-05-02 03:27:00.0
eclipse 搭建SpringMVC框架一

springMVC的搭建环境

?

一、搭建框架,首先新建一个项目工程


直接下一步,完成即可。

?

二、将必需的jar包导入到项目中


三、然后配置web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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">

<display-name>Spring3MVC</display-name>

<welcome-file-list>

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

</welcome-file-list>

?

<servlet>

<servlet-name>spring</servlet-name>

<servlet-class>

org.springframework.web.servlet.DispatcherServlet

</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>spring</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

</web-app>

四、配置spring-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" 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">

?

<!-- <mvc:annotation-driven /> -->

<context:component-scan

base-package="net.viralpatel.spring3.controller" />

?

<bean id="viewResolver"

class="org.springframework.web.servlet.view.UrlBasedViewResolver">

<property name="viewClass"

value="org.springframework.web.servlet.view.JstlView" />

<property name="prefix" value="/WEB-INF/jsp/" />

<property name="suffix" value=".jsp" />

</bean>

</beans>

五、建立jsp界面

index.jsp和jsp/hello.jsp

index.jsp界面:

<html>

<head>

<title>Spring 3.0 MVC Series: Index - ViralPatel.net</title>

</head>

<body>

<a href="hello">Say Hello</a>

</body>

</html>

jsp/hello.jsp

<html>

<head>

<title>Spring 3.0 MVC Series: Hello World - ViralPatel.net</title>

</head>

<body>

${message}

</body>

</html>

六、建立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


?

?

?

?

  相关解决方案