SpringMVC构建WEB应用(一)

一、创建WEB工程项目

    参见之前博文,使用 IDEA GRADLE 创建一个名为 smvcdemo 的 Project。添加Web.XML,转成WEB 工程。

125843_qOTR_2681709.png

二、添加相关包和文件

添加如图所示的包、文件夹和文件

三、配置依赖文件和 web.xml

group 'org.happy'
version '1.0'

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'jetty'

sourceCompatibility = 1.5

repositories {
    //mavenCentral()
    maven { url 'http://maven.oschina.net/content/groups/public/' }
}

dependencies {
    runtime 'javax.servlet:jstl:1.2'
    compile 'org.slf4j:slf4j-log4j12:1.7.7'
    runtime 'mysql:mysql-connector-java:5.1.33'
    compile 'org.springframework:spring-core:4.2.5.RELEASE'
    compile 'org.springframework:spring-beans:4.2.5.RELEASE'
    compile 'org.springframework:spring-context:4.2.5.RELEASE'
    compile 'org.springframework:spring-context-support:4.2.5.RELEASE'
    compile 'org.springframework:spring-web:4.2.5.RELEASE'
    compile 'org.springframework:spring-webmvc:4.2.5.RELEASE'
    compile 'org.springframework:spring-test:4.2.5.RELEASE'
    compile 'log4j:log4j:1.2.17'

    testCompile group: 'junit', name: 'junit', version: '4.11'
}

四、修改 web.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>smvcdemo</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
       <servlet-name>smvcdemo</servlet-name>
       <url-pattern>*.do</url-pattern>
   </servlet-mapping>

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

</web-app>

说明:

    1)load-on-startup:表示启动容器时初始化该Servlet;

    2)url-pattern:表示哪些请求交给Spring Web MVC处理, “/” 是用来定义默认servlet映射的。也可以如“*.do”表示拦截所有以do为扩展名的请求。

    3) 请求需要交给Spring Web MVC框架处理,因此要配置Spring的配置文件,默认DispatcherServlet会加载WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件。我们这里为WEB-INF/ smvcdemo-servlet.xml

五、修改 SpringMVC配置文件

<?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:p="http://www.springframework.org/schema/p"
      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">

       <context:component-scan base-package="org.happy.smvcdemo.controller" />

</beans>

  说明:

    1) <context:componet-scan ...  表示自动 扫描 我们项目 controller 包下的所有 使用@Controller 注解的控制器

六、创建相关的Model

package org.happy.smvcdemo.model;

/**
* Created by  on 16/3/22.
*/
public class Customer {
   private Long id;
   private String name;
   private String contact;
   private String telephone;
   private String address;
   private String remark;
   // 省略 Setter Getter 可在IDEA中自动创建

package org.happy.smvcdemo.model;

/**
 * Created by  on 16/3/22.
 */
public class User {

    private Long id;
    private String name;
    private String passwd;
    private String dept;


七、创建Service 


package org.happy.smvcdemo.service;

import java.util.ArrayList;
import java.util.List;
import org.happy.smvcdemo.model.User;
import org.happy.smvcdemo.model.Customer;

public class CustomerService {

   public List<Customer> getCustomerList() {
       String sql = "SELECT * FROM customer";

       List<Customer> cl = new ArrayList<Customer>();

       Customer c1 = new Customer();
       c1.setId(1L);
       c1.setName("Phonex");
       c1.setContact("David Lee");
       c1.setAddress("No.3 , Haapy Road, Funny district, L.A.");
       c1.setTelephone("+1 2345678");
       c1.setRemark("1st US customer");
       cl.add(c1);

       Customer c2 = new Customer();
       c2.setId(2L);
       c2.setName("Dragon");
       c2.setContact("Lei Lao Hu");
       c2.setAddress("No.7 , Haapy Road, Funny district, Shanghai");
       c2.setTelephone("+86 13912345678");
       c2.setRemark("1st CN customer");
       cl.add(c2);

       Customer c3 = new Customer();
       c3.setId(3L);
       c3.setName("Tiger Sports");
       c3.setContact("Summer Lee");
       c3.setAddress("No.8 , Haapy Road, Funny district, Landon");
       c3.setTelephone("+7 12345678");
       c3.setRemark("1st UK customer");
       cl.add(c3);

       return cl;
   }
}


八、创建controller

  大致的逻辑是:输入(演示用。这里写死的)用户名和密码,通过后,显示客户信息。

   

package org.happy.smvcdemo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class Login {
   // 使用 .../login.do 定位到本控制器,POST请求方式
   @RequestMapping(value = "/login", method = RequestMethod.POST)
   public String login(@RequestParam("username") String username,  
                       @RequestParam("password") String password, Model model) {
       // 获取请求中的 username passpword 参数
       if (username.equals("happy") && password.equals("happy")) {
           model.addAttribute("username", username);
           // 如果匹配,表示登录成功,重定向到 客户信息显示页面
           return "redirect:/listcustomers.do";
       } else {
           // 如果失败 返回登录失败信息
           return "error.jsp";
       }
   }
}
package org.happy.smvcdemo.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.happy.smvcdemo.service.CustomerService;

/**
* Created by  on 16/3/22.
*/
@Controller
public class CustomerController {
   private CustomerService serv;
   // 构造函数获得Service
   public CustomerController() {
       this.serv = new CustomerService();
   }

   @RequestMapping(value = "listcustomers", method = RequestMethod.GET)
   public String listCustomers(Model model) {
       List customerList = serv.getCustomerList();
       // 写入客户List,
       model.addAttribute("customerList", customerList);
       // 重定向到 页面输出客户信息
       return "customers.jsp";
   }
}


说明:

    1) 使用@controller 注解表示这是一个(可以被自动扫描到)的控制器

    2)通过@RequestMapping注解可以用指定的URL访问本控制器,同时可指定请求方式(GET/POST/...)

    3) @RequestParam 根据参数名获取请求参数

 九、前端页面

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form action="login.do" method="post">
 username:
 <input type="text" name="username">
 <br />
 password:
 <input type="text" name="password">
 <br />
 <input type="submit" value="submit">
 <br />
</form>
</body>
</html>

customers.jsp

<%@ page pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
 <title>客户管理</title>
</head>
<body>

<h1>客户列表</h1>

<table>
 <tr>
   <th>客户名</th>
   <th>联系人</th>
   <th>联系电话</th>
   <th>地址</th>
   <th>备注</th>

 </tr>
 <c:forEach var="customer" items="${customerList}">
   <tr>
     <td>${customer.name}</td>
     <td>${customer.contact}</td>
     <td>${customer.telephone}</td>
     <td>${customer.address}</td>
     <td>${customer.remark}</td>
   </tr>
 </c:forEach>
</table>

</body>
</html>

error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
   <title></title>
</head>
<body>
<h1>Login Failed!! Please enter correct user info.</h1>
</body>
</html>



十、试运行

在IDEA中配置一个运行,启动后在浏览器输入 http://localhost:8080/smvcdemo/login.jsp 

分别输入正确的/错误的 用户名密码,即可测试

134104_bQpN_2681709.png

转载于:https://my.oschina.net/ritchielei/blog/645049

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值