`
DavyJones2010
  • 浏览: 153847 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring MVC: Introduction I - Environment Setup

阅读更多

RoadMap:

1) Setup Environment

2) Mapping between Controller and View

3) How to pass value from view to controller

4) How to pass value from controller to view

4) Exception handling

5) Tags in view

6) File upload/download

7) Source code investigation 


 

1. Setup Environment

1> Create a web project and add maven capibility

2> Add dependency for spring mvc

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>edu.xmu.web.WebProject</groupId>
	<artifactId>WebProject-Core</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<properties>
		<spring.version>3.0.5.RELEASE</spring.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<!-- Spring 3 dependencies -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.1.1</version>
				<configuration>
					<webXml>WebContent\WEB-INF\web.xml</webXml>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

3> Add spring-mvc feature in web.xml

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_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>WebProject</display-name>
	
	<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>
</web-app>

4> Add spring-mvc config file in WEB-INF directory

springmvc-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: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.xsd  
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/context/spring-mvc.xsd">
	<!-- Handler Mapping -->
	<bean
		class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping" />

	<bean name="/welcome.html" class="edu.xmu.webproject.controller.WelcomeController"></bean>

	<!-- View Resolver for JSPs -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

</beans>

 5> WelcomeController

package edu.xmu.webproject.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class WelcomeController extends AbstractController
{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception
	{
		System.out.println("Welcome");
		return new ModelAndView("welcome");
	}
}

6> welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h1>Welcome</h1>
</body>
</html>

7> project hierarchy overview


8> Final test



Summary:

  The controller above would make it hard and redundancy when execute user's CRUD request from view,

  because every url mapped to only one controller.

  We will talk about another way to handle this. 

 

  • 大小: 7.4 KB
  • 大小: 16.5 KB
  • 大小: 15.7 KB
分享到:
评论

相关推荐

    SpringMVC源码总结(三)mvc:annotation-driven和mvc:message-converters简单介绍

    http://www.springframework.org/schema/mvc/spring-mvc.xsd"&gt; &lt;mvc:annotation-driven&gt; &lt;mvc:message-converters register-defaults="false"&gt; &lt;!-- 添加自定义或扩展的消息转换器 --&gt; &lt;!-- 添加对XML的支持...

    sentinel-spring-webmvc-adapter-1.8.0-API文档-中文版.zip

    赠送jar包:sentinel-spring-webmvc-adapter-1.8.0.jar; 赠送原API文档:sentinel-spring-webmvc-adapter-1.8.0-javadoc.jar; 赠送源代码:sentinel-spring-webmvc-adapter-1.8.0-sources.jar; 赠送Maven依赖信息...

    拦截器与冲突解决

    1. **配置顺序**:Spring MVC按照注册拦截器的顺序执行它们,如果`&lt;mvc:annotation-driven /&gt;`配置在拦截器之后,那么可能会导致拦截器无法正常工作,因为Spring可能已经处理了请求,而没有交给拦截器。 2. **命名...

    SpringMVC源码总结(二)mvc:mvc:annotation-driven背后的那些事

    在Spring MVC框架中,`mvc:annotation-driven`是Spring MVC配置中的一个重要元素,它使得我们的应用能够支持基于注解的控制器、数据绑定、格式化转换器和服务端验证等功能。这篇博客将深入探讨`mvc:annotation-...

    springfox-spring-webmvc-3.0.0-API文档-中英对照版.zip

    赠送jar包:springfox-spring-webmvc-3.0.0.jar; 赠送原API文档:springfox-spring-webmvc-3.0.0-javadoc.jar; 赠送源代码:springfox-spring-webmvc-3.0.0-sources.jar; 赠送Maven依赖信息文件:springfox-...

    spring-webmvc-5.0.8.RELEASE-API文档-中文版.zip

    赠送jar包:spring-webmvc-5.0.8.RELEASE.jar; 赠送原API文档:spring-webmvc-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-webmvc-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.0.8....

    sentinel-spring-webmvc-adapter-1.8.0-API文档-中英对照版.zip

    赠送jar包:sentinel-spring-webmvc-adapter-1.8.0.jar; 赠送原API文档:sentinel-spring-webmvc-adapter-1.8.0-javadoc.jar; 赠送源代码:sentinel-spring-webmvc-adapter-1.8.0-sources.jar; 赠送Maven依赖信息...

    springfox-spring-webmvc-3.0.0-API文档-中文版.zip

    赠送jar包:springfox-spring-webmvc-3.0.0.jar; 赠送原API文档:springfox-spring-webmvc-3.0.0-javadoc.jar; 赠送源代码:springfox-spring-webmvc-3.0.0-sources.jar; 赠送Maven依赖信息文件:springfox-...

    spring-webmvc-5.3.10-API文档-中文版.zip

    赠送jar包:spring-webmvc-5.3.10.jar; 赠送原API文档:spring-webmvc-5.3.10-javadoc.jar; 赠送源代码:spring-webmvc-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.3.10.pom; 包含翻译后的API...

    spring-webmvc-5.3.15-API文档-中文版.zip

    赠送jar包:spring-webmvc-5.3.15.jar; 赠送原API文档:spring-webmvc-5.3.15-javadoc.jar; 赠送源代码:spring-webmvc-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.3.15.pom; 包含翻译后的API...

    spring-webmvc-5.3.7-API文档-中文版.zip

    赠送jar包:spring-webmvc-5.3.7.jar; 赠送原API文档:spring-webmvc-5.3.7-javadoc.jar; 赠送源代码:spring-webmvc-5.3.7-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.3.7.pom; 包含翻译后的API文档...

    spring-webmvc-5.2.15.RELEASE-API文档-中文版.zip

    赠送jar包:spring-webmvc-5.2.15.RELEASE.jar; 赠送原API文档:spring-webmvc-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-webmvc-5.2.15.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-...

    Spring MVC step-by-step 源码

    Spring MVC 是一个强大的Java Web开发框架,用于构建高效、可维护的Web应用程序。它基于Spring框架,提供了模型-视图-控制器(MVC)架构,简化了开发过程。本资源"Spring MVC step-by-step 源码"是针对初学者准备的...

    spring-webmvc-5.3.7-API文档-中英对照版.zip

    赠送jar包:spring-webmvc-5.3.7.jar; 赠送原API文档:spring-webmvc-5.3.7-javadoc.jar; 赠送源代码:spring-webmvc-5.3.7-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.3.7.pom; 包含翻译后的API文档...

    spring-webmvc-5.3.15-API文档-中英对照版.zip

    赠送jar包:spring-webmvc-5.3.15.jar; 赠送原API文档:spring-webmvc-5.3.15-javadoc.jar; 赠送源代码:spring-webmvc-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.3.15.pom; 包含翻译后的API...

    spring-webmvc-5.2.15.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-webmvc-5.2.15.RELEASE.jar; 赠送原API文档:spring-webmvc-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-webmvc-5.2.15.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-...

    Spring MVC Designing Real-World Web Applications azw3

    Spring MVC Designing Real-World Web Applications 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Spring MVC Designing Real-World Web Applications 无水印pdf 0分

    Spring MVC Designing Real-World Web Applications 英文无水印pdf pdf使用FoxitReader和PDF-XChangeViewer测试可以打开

    Spring MVC Designing Real-World Web Applications epub

    Spring MVC Designing Real-World Web Applications 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    spring-webmvc-5.0.8.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-webmvc-5.0.8.RELEASE.jar; 赠送原API文档:spring-webmvc-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-webmvc-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.0.8....

Global site tag (gtag.js) - Google Analytics