本篇博文是在上篇博文《利用Idea创建web项目 》的基础上来讲的!
1、下面是整个项目的结构。
2、首先在pom文件中加入对应的依赖,文件内容如下。
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>bestpay-gateway</groupId>
<artifactId>gateway-web</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<spring.version>4.0.2.RELEASE</spring.version>
<geronimo-servlet_2.5_spec.version></geronimo-servlet_2.5_spec.version>
<geronimo-servlet_2.5_spec.version>1.2</geronimo-servlet_2.5_spec.version>
<lombok.version>0.11.4</lombok.version>
</properties>
<dependencies>
<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>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_2.5_spec</artifactId>
<version>${geronimo-servlet_2.5_spec.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</project>
3、本项目主要涉及到两个类:LoginController.java和LoginForm.java。
LoginController.java:
package com.bestpay.gateway.controller;
import com.bestpay.gateway.model.LoginForm;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @title LoginController.java
* @description 登录控制器
* @author lichunan
* @date 2014-10-28
* @time 18:12
* @version 1.0
*/
@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;
}
}
LoginForm.java:
package com.bestpay.gateway.model;
import lombok.Data;
/**
* @title LoginForm.java
* @description 登录实体类
* @author lichunan
* @date 2014-10-28
* @time 18:23
* @version 1.0
*/
@Data
public class LoginForm {
private String userName;
private String passWord;
}
4、本项目主要涉及到两个配置文件,分别为applicationContext.xml和spring-mvc.xml。
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
spring-mvc.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"
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">
<!-- 激活@Controller模式 -->
<mvc:annotation-driven/>
<!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 需要更改 -->
<context:component-scan base-package="com.bestpay.*"/>
<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>
5、本项目主要涉及到3个页面,分别为/index.jsp、/WEB-INF/jsp/index/index.jsp、/WEB-INF/jsp/login/login.jsp。
/index.jsp:
<%--
Created by IntelliJ IDEA.
User: lichunan
Date: 14-10-28
Time: 上午10:50
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/jsp/login/login.jsp");
rd.forward(request, response);
%>
<html>
<head>
<title>跳转页面</title>
</head>
<body>
<h2>翼支付门户教程之搭建SpringMvc环境</h2>
</body>
</html>
/WEB-INF/jsp/index/index.jsp:
<%--
Created by IntelliJ IDEA.
User: lichunan
Date: 14-10-28
Time: 下午8:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
${command}
</body>
</html>
/WEB-INF/jsp/login/login.jsp:
<%--
Created by IntelliJ IDEA.
User: lichunan
Date: 14-10-28
Time: 下午8:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<div>
<form action="login" method="get">
<input type="text" name="userName">
<input type="submit" value="SUBMIT">
</form>
</div>
</body>
</html>
6、本项目的web.xml的内容如下。
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
<!-- 启动Web容器时,自动装配ApplicationContext的配置信息 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- ApplicationContext的配置信息路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<!-- 前端控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<!-- 前端控制器的映射路径配置 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>