Fork me on GitHub
余鸢

第一个Spring MVC项目

创建动态Web项目,提供以下信息

Project name : DemoSpringMVCProject
Target runtime:设置为Apache Tomcat v7.0服务器
点击完成,我们成功创建了动态Web项目。

现在我们要设置Spring-MVC框架:

  1. 在WebContent\WEB-INF\文件夹下创建web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Demo9</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
  • 其中DispatcherServlet类拦截传入请求并确定哪个控制器处理请求。
    • 我们将在创建servlet.xml时使用servlet-name’demo’
  1. 在WebContent \ WEB-INF \文件夹下创建demo-servlet.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
  • context:component-scan用于扫描在“com”包下定义的所有控制器。
  • ViewResolver接口用于管理逻辑视图和实际视图之间的映射。 预定义的视图解析器的实现可用于映射视图。 例如:InternalResourceViewResolver,VelocityViewResolver。

  • 要搜索所有的jsp页面,我们定义了前缀,它只是setter属性,它的值设置为’/ WEB-INF / jsp /‘(文件夹路径)。 后缀,这只是getter属性,它的值设置为’.jsp’(扩展名为.jsp的搜索文件)

  1. 添加所需Libraries:

让我们在我们的项目中添加Spring Framework和通用日志API库。 为此,右键单击项目名称DemoSpringMVCProject,然后按照下列选项在上下文菜单中可用:构建路径 - >配置构建路径以显示Java构建路径窗口如下:

现在使用Libraries选项卡下的Add External JARs按钮从Spring Framework和Common Logging安装目录添加以下核心JAR:

  • commons-logging-1.1.1

  • spring-aop-4.1.6.RELEASE

  • spring-aspects-4.1.6.RELEASE

  • spring-beans-4.1.6.RELEASE

  • spring-context-4.1.6.RELEASE

  • spring-context-support-4.1.6.RELEASE

  • spring-core-4.1.6.RELEASE

  • spring-expression-4.1.6.RELEASE

  • spring-instrument-4.1.6.RELEASE

  • spring-instrument-tomcat-4.1.6.RELEASE

  • spring-jdbc-4.1.6.RELEASE

  • spring-jms-4.1.6.RELEASE

  • spring-messaging-4.1.6.RELEASE

  • spring-orm-4.1.6.RELEASE

  • spring-oxm-4.1.6.RELEASE

  • spring-test-4.1.6.RELEASE

  • spring-tx-4.1.6.RELEASE

  • spring-web-4.1.6.RELEASE

  • spring-webmvc-4.1.6.RELEASE

  • spring-webmvc-portlet-4.1.6.RELEASE

  • spring-websocket-4.1.6.RELEASE

让我们转向控制器和jsp页面:

  1. 在src文件夹下创建一个com.demo.controller包。
  2. 在com.demo.controller包下创建一个LoginController类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.demo.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class LoginController {
@RequestMapping("/")
public String startPage(){
return "login";
}
@RequestMapping("/singin")
public String handleRequest(HttpServletRequest request){
String name = request.getParameter("name");
String pass = request.getParameter("password");
if(name.equals(pass))
{
return "welcome";
}else{
return "login";
}
}
}
  1. 在“WebContent \ WEB-INF \ jsp \’下创建一个login.jsp和welcome.jsp页面

login.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<form action="singin">
<table>
<tr>
<td>User Name : </td>
<td><input type="text" name="name" id="name"/> </td>
</tr>
<tr>
<td>Password: </td>
<td><input type="text" name="password" id="password"/> </td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Login"/></td>
</tr>
</table>
</form>
</body>
</html>

welcome.jsp

1
2
3
4
5
6
7
8
9
10
11
12
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1> Welcome to Spring MVC !!! </h1>
</body>
</html>

在localTomcat服务器中添加DemoSpringMVCProject并在服务器上运行它。

备注

本文概述了spring-mvc是什么,以及为什么开发人员可能想使用它。