使用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.项目的目录结构


至此简单的SpringMVC搭建完毕,要学习更多SpringMVC的知识,请访问Spring官网(http://spring.io/guides/gs/serving-web-content/,http://docs.spring.io/spring/docs/4.1.0.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#mvc-introduction
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值