Personally, there are two best ways to learn the framework: one is to practice the framework and use it for real, such as the spring framework, first build a simple spring project, and then enrich it step by step, learn from it The essence and knowledge of the spring framework; the other is to look at the source code, which has been recommended by countless great gods, but he needs a certain ability (at least you must have a certain knowledge accumulation of design patterns and sufficient java coding ability).
Simply implement springmvc today:
First, create a maven-archetype-webapp project, the path diagram is as follows:
pom file, import the required jar package:
<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's basic dependencies start --> <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> <!-- end of spring's basic dependencies--> </dependencies> <build> <finalName>test</finalName> </build> <artifactId>test</artifactId> </project>
And then the web.xml file:
<?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"> <!-- Distinguish the project name to prevent the default duplicate name--> <context-param> <param-name>webAppRootKey</param-name> <param-value>maven.cainiao.root</param-value> </context-param> <!-- Spring's log4j listener --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- charset filter--> <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 dispatcher--> <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: (this is c in mvc, control layer)
<?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 class: (m in mvc, model layer)
package com.beans; /** * User : User bean * @author xuejupo [email protected] * * create in 2016-3-1 4:23:16 pm */ 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 "User name:\r\n"+this.userName + "User password:\r\n"+this.passwd + "User information:\r\n"+ this.info; } }
The controller DispatcherController (together with the dispatcher_cainiao.xml file forms the control layer, which controls which part of the code logic and which model layer should be used for the incoming url):
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 PM * */ @Controller @RequestMapping("demo") public class DispatcherController { @RequestMapping(method=RequestMethod.GET) public String printWelcome(ModelMap model) { User user = new User(); user.setInfo("Haha, I'm the only user!"); user.setUserName("I am the boss!"); user.setPasswd("Don't tell you!"); model.addAttribute("str0121", "I'll go, it's a success!!!"); model.addAttribute("info","The current user information is:"); model.addAttribute("user", user); System.out.println("index.jsp"); return "index"; } }
index.jsp (the v layer in mvc, the display layer, the layer responsible for displaying the results to the front end and dealing directly with users)
<%@ 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>
Then enter in the url: http://localhost:8080/test/demo
result:
PS: There were two bugs during the build process.
1. Because I am using jboss, I got this error when publishing:
java.lang.IllegalStateException: Could not detect JBoss VFS infrastructure
The online query is that spring4 has removed the VFS support for jboss. . . . So I use spring3 in my pom.
2.No mapping found for HTTP request with URI
Add this dependency to the pom:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
That's it. . The reason is temporarily unknown. Online query spring-tx provides transaction management functions and support for JAO and DAO functions. But the specific reason is still unknown, please leave a message to let the gods know. . .
Only update these for the time being today, and next time I will analyze the role of each configuration file and the specific process of access.
PS: Welcome to reprint, please indicate the source of reprint: