Maven3: A Simple Example of Building SpringMVC with Maven3 博客分类: Maven Maven3SpringMVCSimple Demo
程序员文章站
2024-03-20 11:41:58
...
1. A glimpse of Dir Hierarchy
2. pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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.mavenspring</groupId> <artifactId>Maven-SpringMVC</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <properties> <spring.version>3.2.2.RELEASE</spring.version> </properties> <dependencies> <!-- Dependencies for jUnit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.3.1</version> <scope>test</scope> </dependency> <!-- Dependencies for SpringMVC --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <!-- Change default compile level --> <build> <finalName>Maven-SpringMVC</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>
2. web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>springapp</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springapp</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3. springapp-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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean name="/fileUpload" class="edu.xmu.mavenspring.controls.FileUploadController"> </bean> </beans>
4. FileUploadController.java
package edu.xmu.mavenspring.controls; 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 FileUploadController implements Controller { public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { // TODO Auto-generated method stub ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("message", "Congratulations!"); modelAndView.setViewName("Succeed.jsp"); System.out.println("Inside controller"); return modelAndView; } }
5. Succeed.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>Upload Succeed</title> </head> <body> <h1>Upload Succeed!</h1><br/> ${message} </body> </html>
Comments;
1. This is just a simple demo for showing how to build dependencies for springMVC in Maven.
2. And only use a simple fraction of springMVC capablities.
3. Remember to include jstl dependencies in pom.xml for ${message} to be shown correctly.
4. Useful links:
1) http://jinnianshilongnian.iteye.com/category/231099?page=2 ---> Show SpringMVC in details
2) http://mvnrepository.com/ ---> Where we can find dependencies <groupId & artifactId & version>