Skip to content

Commit

Permalink
add-hw
Browse files Browse the repository at this point in the history
  • Loading branch information
wenxiaoning committed Jan 19, 2018
1 parent 9f866c3 commit 32fa79f
Show file tree
Hide file tree
Showing 13 changed files with 371 additions and 0 deletions.
24 changes: 24 additions & 0 deletions java/SpringMVC-HelloWorld/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
*.swp
*.swo
21 changes: 21 additions & 0 deletions java/SpringMVC-HelloWorld/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 wxnacy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
187 changes: 187 additions & 0 deletions java/SpringMVC-HelloWorld/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
# 使用 Maven 构建 SpringMVC :Hello World

本文简单介绍如何使用 Maven 框架搭建并配置 StringMVC,完成输入 Hello World 的功能。
<!-- more --><!-- toc -->
看本文前最好了解 Maven 和 Spring 基本知识
## Maven 创建 Web 项目
```bash
mvn archetype:generate -DgroupId=com.wxnacy.spring -DartifactId=SpringMVC-HelloWorld -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
```
首先使用上述命令创建一个空的 Web 项目,结构如下

![1](springhw1.png)

## 创建 Controller 和 View
首先修改 `pom.xml` 文件,添加 SpringMVC 的依赖以及 Tomcat7 的插件
```xml
<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.wxnacy.spring</groupId>
<artifactId>SpringMVC-HelloWorld</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringMVC-HelloWorld Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.0.1.RELEASE</spring.version>
</properties>

<dependencies>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>SpringMVC-HelloWorld</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
<!--<finalName>mgr</finalName>-->
<server>tomcat7</server>
</configuration>
</plugin>

</plugins>
</build>
</project>
```
接下来创建 Controller
```bash
$ mkdir -p src/main/java/com/wxnacy/spring/controller
$ touch src/main/java/com/wxnacy/spring/controller/HelloWorldController.java
```
HelloWorldController.java
```java
package com.wxnacy.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class HelloWorldController {

@RequestMapping("/hello")
public ModelAndView helloWorld(
@RequestParam(value = "name", required = false, defaultValue = "World") String name
) {
ModelAndView mv = new ModelAndView("index");
mv.addObject("name", name);
return mv;
}
}
```
使用注解 `@Controller` 来定义该类为控制器,`@RequestMapping` 映射访问地址 `/hello``helloWorld` 方法中,该方法接收一个参数,并返回 Model。

最后创建 View
```bash
$ mkdir src/main/webapp/WEB-INF/views
$ touch src/main/webapp/WEB-INF/views/index.jsp
```
index.jsp
```html
<html>
<body>
<h2>Hello ${name}!</h2>
</body>
</html>
```
## 配置 SpringMVC
原料齐全了,接下来需要将他们关联起来,首先在 xml 文件中配置项目启动时扫描的 Controller 和 View 的目录
```bash
$ touch src/main/resources/dispatcher.xml
```
dispatcher.xml
```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"
xsi:schemaLocation="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.2.xsd">


<context:component-scan base-package="com.wxnacy.spring.controller"/>

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
```
然后在 web.xml 中设置 Servlet,用 `/` 地址映射刚才的配置文件。
```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_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<display-name>Archetype Created Web Application</display-name>
</web-app>
```
此时项目目录结构为
![2](/springhw2.png)

启动项目
```bash
$ mvn tomcat7:run
```
此时访问地址 `http://localhost:8080/hello` 出现如下效果即为成功
![3](/springhw3.png)
![4](/springhw4.png)

更多文章:https://wxnacy.com/
59 changes: 59 additions & 0 deletions java/SpringMVC-HelloWorld/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<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.wxnacy.spring</groupId>
<artifactId>SpringMVC-HelloWorld</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringMVC-HelloWorld Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.0.1.RELEASE</spring.version>
</properties>

<dependencies>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>SpringMVC-HelloWorld</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
<!--<finalName>mgr</finalName>-->
<server>tomcat7</server>
</configuration>
</plugin>

</plugins>
</build>
</project>
Binary file added java/SpringMVC-HelloWorld/springhw1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added java/SpringMVC-HelloWorld/springhw2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added java/SpringMVC-HelloWorld/springhw3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added java/SpringMVC-HelloWorld/springhw4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.wxnacy.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Controller
public class HelloWorldController {

@RequestMapping("/hello")
public ModelAndView showMessage(
@RequestParam(value = "name", required = false, defaultValue = "World") String name
) {
ModelAndView mv = new ModelAndView("index");
mv.addObject("name", name);
return mv;

}

}
22 changes: 22 additions & 0 deletions java/SpringMVC-HelloWorld/src/main/resources/dispatcher.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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"
xsi:schemaLocation="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.2.xsd">


<context:component-scan base-package="com.wxnacy.spring.controller"/>

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<h2>Hello ${name}!</h2>
</body>
</html>

22 changes: 22 additions & 0 deletions java/SpringMVC-HelloWorld/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<display-name>Archetype Created Web Application</display-name>
</web-app>
5 changes: 5 additions & 0 deletions java/SpringMVC-HelloWorld/src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

0 comments on commit 32fa79f

Please sign in to comment.