通用技术 新手入门-如何搭建 SpringMVC 框架

王华 · November 28, 2016 · Last by Nan replied at November 28, 2016 · 1353 hits

最近项目不是很紧急,闲下来了想学一下web和数据库交互,听说SpringMVC框架是个轻量级的框架,第一步先搭建框架:

第一步:创建Maven工程
.

第二步:配置POM.xml

下载SpringMVC需要的依赖包
这个是我的pom.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.njiang</groupId>
<artifactId>nan</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>nan Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>

<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_2.5_spec</artifactId>
<version>1.2</version>
</dependency>

</dependencies>
<build>
<finalName>nan</finalName>
</build>
</project>

这个配置完成后,进入整个工程名,点击:update Maven ,此时会触发下载依赖包,依赖包下载下来如下图:

第三步:

配置servelt.xml

  1. 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.3" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_3.xsd"
>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>display2</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>display2</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

  1. 配置 display2-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Bean头部 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
>

<!-- 激活@Controller模式 -->
<mvc:annotation-driven />
<!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 需要更改 -->
<context:component-scan base-package="monitor" />

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<bean id="
viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="
prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="
suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
  1. 配置 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
</beans>

第四步: 以上三步配置完成后,开始写几个简单的jsp文件和处理文件

整个目录结构是这样的:

1 > 最外层的index.jsp nan/src/main/webapp/index.jsp


<%
request.getRequestDispatcher("/WEB-INF/jsp/login/login.jsp").forward(request,response);
%>

2 > webapp/WEB-INF/jsp/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">
<%@ page isELIgnored="false" %>
<title>Insert title here</title>
</head>
<body>
${command}
</body>
</html>

3 > /main/webapp/WEB-INF/jsp/login.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>
<div>
<form action="login" methed="get">
<input type="text" name="username">
<input type="submit" value="SUBMIT">
</form>
</div>
</body>
</html>

4 > /src/main/java/domain/LoginForm.java


package domain;


public class LoginForm {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

5 > nan/src/main/java/monitor/LoginController.java


package monitor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import domain.LoginForm;

@Controller
public class LoginController {
@RequestMapping(value="login")
public ModelAndView login(HttpServletRequest request,HttpServletResponse response,LoginForm command ){
String username = command.getUsername();
ModelAndView mv = new ModelAndView("index/index","command","LOGIN SUCCESS, " + username);
return mv;
}
}

那么问题来了,用什么服务器执行?
网上有推荐使用tomact服务器,我是用的是Eclipse的一个插件,Jetty

如何下载:

进入Eclipse-Help-Eclipse MarketPlace

输入: run-jetty-run

jetty有1.7、1.8、1.9三个版本,对应不同jdk版本(我本地装的jdk 1.8.0版本,装的jetty是1.9版本 可以运行)

装好后设置一下jetty的版本

下面看下运行的结果:

一个很简单的web工程搭建起来了~

共收到 1 条回复 时间 点赞

学习学习!

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up