使用Maven構建SpringMVC

1.新建一個web項目

參考之前的博文使用Maven構建web項目這一章節

2.修改pom.xml,添加Spring依賴

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.carson.springmvc</groupId>
	<artifactId>springmvc</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>springmvc Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.9</version>
			<scope>test</scope>
		</dependency>
		<!--spring MVC依賴 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>
	    <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>
<!-- 		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-asm</artifactId>
			<version>3.1.4.RELEASE</version>
		</dependency> -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
			<scope>compile</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>springmvc</finalName>
		<outputDirectory>${basedir}/src/main/webapp/WEB-INF/classes</outputDirectory>
	</build>
</project>

3.添加spring的配置文件,applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"   
       xmlns:aop="http://www.springframework.org/schema/aop"   
       xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:mvc="http://www.springframework.org/schema/mvc"  
       xmlns:tx="http://www.springframework.org/schema/tx"   
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
        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/tx   
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  
  
    <!-- SpringMVC annotation -->
    <mvc:annotation-driven />
    
    <!-- Spring annotation --> 
    <context:component-scan base-package="com.carson.springmvc.*" />  
  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/" />  
        <property name="suffix" value=".jsp" />  
    </bean>  
  
</beans>  

4.配置web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
  <!-- 解決post中文亂碼 -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- Spring 監聽,初始化上下文 -->
<!--     <listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener> 
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param> -->
	
	<!--  Spring view分發器-->
    <servlet>  
        <servlet-name>student</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
        <init-param>
        <!-- DispatcherServlet初始化上下文 -->
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
    </servlet>  
    <servlet-mapping>  
        <servlet-name>student</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>
	
	<!-- log4j -->
	<context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
    
    
    
</web-app>


這裏我寫的不太規範,我看了網上大部分資料都是把spring的配置跟springMVC的配置分開寫,我這裏是爲了偷懶。大家寫的時候可以把applicationContext.xml中Spring與SpringMVC部分分開。這樣只要把web.xml中的Spring監聽的配置註解去掉,另外把DispatcherServlet中的配置文件名稱改成你新增的springmvc的配置文件即可。

5.測試

StudentController.java
package com.carson.springmvc.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.carson.springmvc.model.Student;
import com.carson.springmvc.service.StudentService;

@Controller
@RequestMapping("/student")
public class StudentController {

	@Resource
	private StudentService studentService;
	
	@RequestMapping(params = "method=add",value="/{name}")
	public String add(@ModelAttribute("student") Student student){
		System.out.println("-------------SpringMVC in coming---------");
		System.out.println("add student's name is "+student.getName());
		studentService.save(student);
		return "index";
	}
	
	@RequestMapping(params = "method=update")
	public String update(Student student){
		System.out.println("-------------update student---------");
		studentService.update(student);
		return "success";
	}

	@RequestMapping(value="/{id}", method=RequestMethod.GET)
	public String findStudent(@PathVariable String id,Student student){
		System.out.println(id);
		return null;
	}
	
	@RequestMapping(params = "method=delete")
	public String delete(@RequestParam String name){
		System.out.println(name);
		return null;
	}
	
	public StudentService getStudentService() {
		return studentService;
	}

	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}
}


StudentService.java
package com.carson.springmvc.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.carson.springmvc.dao.StudentDao;
import com.carson.springmvc.model.Student;

@Service("studentService")
public class StudentService {

	@Resource
	private StudentDao studentDao;
	
	public void save(Student student){
		studentDao.add(student);
	}

	public void update(Student student){
		studentDao.update(student);
	}
	
	public StudentDao getStudentDao() {
		return studentDao;
	}

	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}
	
	
}


StudentDao.java
package com.carson.springmvc.dao;

import org.springframework.stereotype.Service;

import com.carson.springmvc.model.Student;

@Service
public class StudentDao {

	public void add(Student student){
		System.out.println("--------add student--------");
	}
	
	public void update(Student student){
		System.out.println("--------update student--------");
	}
}


Student.java
package com.carson.springmvc.model;

public class Student {

	private String name;
	
	private int age;
	
	private String sex;
	
	private String classno;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getClassno() {
		return classno;
	}

	public void setClassno(String classno) {
		this.classno = classno;
	}
	
	
}

啓動,訪問http://localhost:8080/springmvc/student/zhang?method=add打印“add student's name is zhang”。SpringMVC也是rest ful風格,可以根據訪問的路徑找到對應的方法,也可以把方法名當成參數來執行對應的方法。如訪問http://localhost:8080/springmvc/student/1會執行findStudent方法。

6.項目的目錄結構

發佈了50 篇原創文章 · 獲贊 48 · 訪問量 45萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章