最近项目不是很紧急,闲下来了想学一下 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
<?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>
<?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>
<?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 工程搭建起来了~