springmvc搭建web项目

工具:eclipse(我用的而是霓虹灯版本,JDK最低1.8否则打不开eclipse)
JDK1.7
Tomcat1.7
1、创建工程

工程名字为 xiaotb
2、导入jar包
Spring-framework,官网上大家可自行下载

Lib包下你可以看到还有4个包不属于Spring范围内,主要是因为在搭建过程中会碰到一些错误,需要这些包的支持依赖,所以可以暂且不管。
3、配置web.xml

<?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"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="WebApp_ID" version="3.0">
  <display-name>xiaotb</display-name>
  <!-- 默认欢迎界面 -->
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <!--使用Spring MVC,配置DispatcherServlet是第一步。DispatcherServlet是一个Servlet,
            所以可以配置多个DispatcherServlet;DispatcherServlet是前置控制器,配置在web.xml文件中的。
            拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller(我们写的Action)来处理。 -->
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--是启动顺序,让这个Servlet随Servletp容器一起启动。-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <!--这个Servlet的名字是dispatcher,可以有多个DispatcherServlet,是通过名字来区分的。每一个DispatcherServlet有自己的WebApplicationContext上下文对象。同时保存的ServletContext中和Request对象中.-->  
    <!--ApplicationContext是Spring的核心,Context我们通常解释为上下文环境,我想用“容器”来表述它更容易理解一些,ApplicationContext则是“应用的容器”了:P,Spring把Bean放在这个容器中,在需要的时候,用getBean方法取出-->  
    <servlet-name>springMVC</servlet-name>
    <!--Servlet拦截匹配规则可以自已定义,当映射为@RequestMapping("/user/add")时,为例,拦截哪种URL合适?-->  
    <!--1、拦截*.do、*.htm, 例如:/user/add.do,这是最传统的方式,最简单也最实用。不会导致静态文件(jpg,js,css)被拦截。-->  
    <!--2、拦截/,例如:/user/add,可以实现现在很流行的REST风格。很多互联网类型的应用很喜欢这种风格的URL。弊端:会导致静态文件(jpg,js,css)被拦截后不能正常显示。 -->  
    <url-pattern>/</url-pattern>
    <url-pattern>/xiaotb/login</url-pattern>
  </servlet-mapping>

  <!--指明了配置文件的文件名,不使用默认配置文件名,而使用dispatcher-servlet.xml配置文件。-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <!--其中<param-value>**.xml</param-value> 这里可以使用多种写法-->  
    <!--1、不写,使用默认值:/WEB-INF/<servlet-name>-servlet.xml-->  
    <!--2、<param-value>/WEB-INF/classes/dispatcher-servlet.xml</param-value>-->  
    <!--3、<param-value>classpath*:dispatcher-servlet.xml</param-value>-->  
    <!--4、多个值用逗号分隔-->
    <!-- <param-value>/WEB-INF/applicationContext.xml</param-value> -->
    <param-value>/WEB-INF/springMVC-servlet.xml</param-value>
  </context-param>
</web-app>

4、配置springMVC-servlet.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util-4.3.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.3.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

  <!-- 使用默认的注解映射 -->
  <mvc:annotation-driven>
    <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
    </mvc:message-converters>
  </mvc:annotation-driven>

  <!-- 自动扫描controller包中的控制器 -->
  <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
  <context:component-scan base-package="com.xiaotb.controller" />
  <context:component-scan base-package="com.xiaotb.service" />
  <context:component-scan base-package="com.xiaotb.dao" />

  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name = "prefix" value = "/WEB-INF/views" />
    <property name = "suffix" value = ".jsp" />
  </bean>

</beans>

5、创建package和对应的java类

UserController.java

package com.xiaotb.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xiaotb.entity.User;
import com.xiaotb.service.UserService;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value="/login")
    @ResponseBody
    public List<User> login(){
        // 获取登录信息
        List<User> userList = userService.findUserList();
        for(User user:userList){
            System.out.println(user.getName()+":"+user.getPwd());
        }
        return userList;
    }
}

UserDao.java

package com.xiaotb.dao;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.xiaotb.entity.User;

@Repository
public class UserDao {

    private User user;

    public List<User> findUserList(){
        List<User> userList = new ArrayList<User>();
        for(int i = 0;i<3;i++){
            user = new User();
            user.setName("张三"+i);
            user.setPwd("A"+i);
            userList.add(user);
        }
        return userList;
    }
}

UserService.java
package com.xiaotb.service;

import java.util.List;
import com.xiaotb.entity.User;

public interface UserService {

    public List<User> findUserList();
}

UserServiceImpl.java

package com.xiaotb.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xiaotb.dao.UserDao;
import com.xiaotb.entity.User;
import com.xiaotb.service.UserService;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserDao userDao;

    @Override
    public List<User> findUserList() {
        // 模拟从数据库获取数据
        List<User> userList = userDao.findUserList();
        return userList;
    }

}

User.java

package com.xiaotb.entity;

import java.io.Serializable;

public class User implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 966360466074318736L;
    private String name = "";
    private String pwd = "";
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }


}

6.创建jsp界面
login.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>登录界面</title>
</head>
<body>
    <c:forEach items="${userList }" var="userItem">
        <!-- 这里使用jstl取值,如果我们把User对象的字段,所对应的getter和setter方法去掉,页面会报错 -->
        -----><a href="${userItem.name }">${userItem.pwd }        </a>
        </br>
    </c:forEach>
</body>
</html>

7、启动tomcat
输入http://localhost:8080/xiaotb/login

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值