準備工具:
eclipse-jee-mars-2-win32-x86_64
apache-tomcat-6.0.48
SpringMVC需要的jar包
SpringMVC簡介
1、SpringMVC和Struts一樣是一個MVC框架,和Spring無縫連接。和Struts2有點相似。
2、SpringMVC屬于SpringFrameWork的后續產品,Spring框架提供了構建Web應用程序的全功能MVC模塊。
3、使用Spring可插入的MVC架構,可以選擇是使用的內置的Spring web框架還可以是Struts這樣的Web框架。
新建一個SpringMVC項目
1、file-->new-->Dynamic Web Project-->SpringMVC1
2、選擇Apache Tomcat v6.0,配置好服務。
image.png
3、Finish
4、生成一個SpringMVC1 Project
image.png
5、添加 lib
image.png
6、web.xml 配置
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
image.png
7、control類:HelloWorldController.java
package com.tiany.web.controller;
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 HelloWorldController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
System.out.println("----hello SpringMVC------");
// TODO Auto-generated method stub
return new ModelAndView("/welcome");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
image.png
8、spring-servlet.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- Bean頭部 -->
<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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean name="/test1/helloworld" class="com.tiany.web.controller.HelloWorldController"></bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>-->
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
image.png
詳細解析:http://www.cnblogs.com/yw0219/p/6086571.html
9、apache 配置
image.png
image.png
啟動后報錯:
image.png
重命名下名稱
springMVC-servlet.xml
image.png
查看頁面
http://localhost:8080/SpringMVC1/test1/helloworld
image.png
千里之行始于足下,just hello world!