springmvc的一个小例子(一)
个人觉得,学框架最好的 方法无外乎两个:一个是实践这个框架,真实的去用它,比如spring框架,先搭一个简单的spring项目,然后一步一步得去丰富它,从中学到spring框架的精髓和知识;另外一个就是看源码了,这个被无数大神推荐过,不过他需要一定的能力(至少你要有一定的设计模式的知识积累和足够牛的java coding能力)。
今天就简单实现一下springmvc:
首先,创建一个maven-archetype-webapp项目,路径图如下:
pom文件,导入需要的jar包:
<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.xuejupo</groupId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>test Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>3.2.4.RELEASE</spring.version> </properties> <dependencies> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12-beta-3</version> <scope>test</scope> </dependency> <!-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- spring 的基本依赖 开始 --> <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-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</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-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</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-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- spring 的基本依赖 结束 --> </dependencies> <build> <finalName>test</finalName> </build> <artifactId>test</artifactId> </project>
然后是web.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app 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" version="2.5"> <!-- 区分项目名称,防止默认重名 --> <context-param> <param-name>webAppRootKey</param-name> <param-value>maven.cainiao.root</param-value> </context-param> <!-- Spring的log4j监听器 --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- 字符集 过滤器 --> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring view分发器 --> <servlet> <servlet-name>dispatcher_cainiao</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher_cainiao.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher_cainiao</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
dispatcher_cainiao.xml:(这就是mvc中的c,控制层)
<?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"> <mvc:annotation-driven /> <!-- <mvc:default-servlet-handler/> --> <context:component-scan base-package="controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/" /> <property name="suffix" value=".jsp" /> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean> </beans>
user类:(mvc中的m,模型层)
package com.beans; /** * User : 用户bean * @author xuejupo [email protected] * * create in 2016-3-1 下午4:23:16 */ public class User { public final String getUserId() { return userId; } public final void setUserId(String userId) { this.userId = userId; } public final String getUserName() { return userName; } public final void setUserName(String userName) { this.userName = userName; } public final String getPasswd() { return passwd; } public final void setPasswd(String passwd) { this.passwd = passwd; } public final String getInfo() { return info; } public final void setInfo(String info) { this.info = info; } private String userId; private String userName; private String passwd; private String info; @Override public String toString(){ return "用户名:\r\n"+this.userName + "用户密码:\r\n"+this.passwd + "用户的信息:\r\n"+ this.info; } }
控制器DispatcherController(和dispatcher_cainiao.xml文件一起组成控制层,控制着传入的url应该使用哪部分代码逻辑和使用哪个model层):
package controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.test.beans.User; /** * DispatcherController: * @author xuejupo [email protected] * * create in 2016-3-1 下午3:35:13 * */ @Controller @RequestMapping("demo") public class DispatcherController { @RequestMapping(method=RequestMethod.GET) public String printWelcome(ModelMap model) { User user = new User(); user.setInfo("哈哈,我是唯一的用户!"); user.setUserName("我是老大!"); user.setPasswd("不告诉你!"); model.addAttribute("str0121", "我去,成功了呀!!!"); model.addAttribute("info","当前用户信息为:"); model.addAttribute("user", user); System.out.println("index.jsp"); return "index"; } }
index.jsp(mvc中的v层,展示层,负责向前端展示结果,直接和用户打交道的层)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!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> <c:out value="${str0121}"></c:out> <p> <c:out value="${info}"></c:out> <p> <c:out value="${user}"></c:out> </body> </html>
然后url中输入:http://localhost:8080/test/demo
结果:
PS:搭建过程中出了两个bug。
1. 因为我用的是jboss,在发布的时候出了这个错:
java.lang.IllegalStateException: Could not detect JBoss VFS infrastructure
网上查询是spring4去掉了对jboss的VFS支持。。。。 所以我的pom里面用的都是spring3.
2.No mapping found for HTTP request with URI
在pom里加入这个依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
就解决了。。 原因暂时不明,网上查询spring-tx提供事务管理功能,提供对JAO和DAO功能的支持。但是具体为啥还未知,欢迎大神留言告知。。。
今天暂只更新这些,下次再具体分析各个配置文件的作用和访问的具体流程。
PS:欢迎转载,转载请标明出处: