Spring MVC 笔记 1

1.新建maven 工程
mvn archetype:generate -DinteractiveMode=false -DarchetypeGroupId=org.codehaus.mojo.archetypes  -DarchetypeArtifactId=webapp-jee5  -Dversion=0.0.1-SNAPSHOT -DgroupId=com.kxmgc  -DartifactId=springmvc

2.修改pom.xml,增加spring mvc 3.0.5的依赖包及jetty plugin
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-webmvc</artifactId>
	<version>3.1.2.RELEASE</version>
</dependency>
<plugin>
	<groupId>org.mortbay.jetty</groupId>
	<artifactId>maven-jetty-plugin</artifactId>
	<version>6.1.7</version>
	<configuration>
		<scanIntervalSeconds>0</scanIntervalSeconds>
	</configuration>
</plugin>

3.修改web.xml,增加如下配置
<servlet>
	<servlet-name>dispatcherContext</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:dispatcherContext-servlet.xml</param-value>
	</init-param> 
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>dispatcherContext</servlet-name>
	<url-pattern>*.action</url-pattern>
</servlet-mapping>

4.在src\resources下,新建dispatcherContext-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
	<context:component-scan base-package="com.kxmgc.springmvc.action" />
    <mvc:annotation-driven />
    	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

</beans>

5.新建HelloworldController类
package com.kxmgc.action;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/helloworld")
public class HelloworldController {
	@RequestMapping(method=RequestMethod.GET) 
	public ModelAndView hello() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		return mv;
	}
}

6.在/WEB-INF/jsp目录下,新建hello.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

7.运行
引用
mvn clean compile jetty:run -Djetty.port=80

猜你喜欢

转载自xml.iteye.com/blog/1700869