项目结构
spring mvc 版本4.04
我们基于Spring mvc框架进行开发,需要依赖一下的spring jar包:
maven pom文件
<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>x-wechat</groupId>
<artifactId>x-wechat</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>x-wechat</name>
<description>x-wechat</description>
<url>http://maven.apache.org</url>
<!-- 引入jar 的版本号 -->
<properties>
<!-- spring版本号 -->
<spring.version>4.0.4.RELEASE</spring.version>
<!-- mybatis版本号 -->
<!-- <mybatis.version>3.2.6</mybatis.version>
<hibernate.version>3.6.9.Final</hibernate.version> -->
<!-- mysql 版本号-->
<!-- <mysql.version>5.1.34</mysql.version> -->
<!-- log4j日志文件管理包版本 -->
<!-- <slf4j.version>1.7.7</slf4j.version> -->
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</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>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- 导入java ee jar 包 -->
<!-- <dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency> -->
</dependencies>
<!--TODO 针对一个项目的编译环节 -->
<build>
<!-- 配置资源文件 -->
<!-- <resources>
<resource>
<directory>src/main/resources</directory>
资源根目录排除各环境的配置,防止在生成目录中多余其它目录
<excludes>
<exclude>development/*</exclude>
<exclude>test/*</exclude>
<exclude>production/*</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/${profiles.active}</directory>
</resource>
</resources> -->
<!--TODO finalName 属性是你项目的名字 -->
<finalName>x-wechat</finalName>
<plugins>
<!--TODO jdk 1.7 编译操作的定义 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- TODO Maven的打包部署与Debug行为描述 -->
<!-- <plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration> 有这个定义,则你调试的时候不会进入target目录下,你就不用总去重启服务,尤其在页面调试的时候很方便
<webappDirectory>${basedir}/src/main/webapp</webappDirectory>
<warSourceDirectory>${basedir}/src/main/webapp</warSourceDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin> -->
<!--Maven Clean 操作 -->
<!-- <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<filesets>
<fileset>
<directory>${basedir}/src/main/webapp/WEB-INF/</directory>
<excludes> 排除哪些内容不被清除
<excluse>**/web.xml</excluse>
</excludes>
</fileset>
</filesets>
</configuration>
</plugin> -->
</plugins>
</build>
</project>
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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- 配置DispatchcerServlet -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置Spring mvc下的配置文件的位置和名称 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicactionContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
springmvc 配置 applictacionContext.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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.cn.xing.action"></context:component-scan>
<!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value="/views/"></property>
<property name = "suffix" value = ".jsp"></property>
</bean>
</beans>
所有的配置文件级本上弄的差不多了,
Java
首先要在类的前面添加“Controller”注解,表示是spring的控制器,这里会写一个方法hello()
2. hello方法上方有一个@RequestMapping, 是用于匹配请求的路径,比如这里匹配的请求路径就是“http://localhost:8080/x-wechat”,即当tomcat服务启动后,在浏览器输入这个url时,点击hell world,如果在这个方法打断点了,就会跳入该方法。
3. 这个return的结果不是乱写的,这个返回的字符串就是与上面applicactionContext.xml中line15-18进行配合的,springmvc.xml中声明了prefix和suffix,而夹在这两者之间的就是这里返回的字符串,所以执行完这个方法后,我们可以得到这样的请求资源路径“/views/success.jsp”,这个success.jsp是需要我们新建的
package com.cn.xing.action;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
*user : xu
*date :2017年7月5日
*time : 下午4:47:17
*/
//标明这是一个控制层类
@Controller
public class TestAction {
/**
* 1. 使用RequestMapping注解来映射请求的URL
* 2. 返回值会通过视图解析器解析为实际的物理视图, 对于InternalResourceViewResolver视图解析器,会做如下解析
* 通过prefix+returnVal+suffix 这样的方式得到实际的物理视图,然后会转发操作
* "/WEB-INF/views/success.jsp"
* @return
*/
@RequestMapping("/helloworld")
public String hello(){
System.out.println("hello world");
return "success";
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
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>Insert title here</title>
</head>
<body>
<a href="helloworld">hello world</a>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
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>登录界面</title>
</head>
<body>
<h1>ninininni</h1>
</body>
</html>
可能会出现404 的情况
错误代码提示
No mapping found for HTTP request with URI [/x-wechat/helloworld] in springDispatcherServlet