在eclipse中创建springMVC项目

1.创建一个Java web项目

 2.输入项目名称,下一步

 3.下一步,勾选创建web.xml选项

 4.在lib文件夹中添加jar包

链接:https://pan.baidu.com/s/1AjhcveNCVokizsrrF_-YLA?pwd=gza8
提取码:gza8
复制这段内容后打开百度网盘手机App,操作更方便哦

5.在WEB-INF文件夹下添加springmvc框架配置文件,起名为springmvc-servlet.xml

 代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
         https://www.springframework.org/schema/mvc/spring-mvc.xsd">
     <mvc:annotation-driven/>
      
    <context:component-scan base-package="com.mvc.web"/>

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

6.配置web.xml文件,该文件时整个项目工程的配置文件,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>springmvc3</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 的前端控制器-->
<servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
<!--        初始化参数,指定springMVC配置文件的路径-->
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
    </init-param>
<!--    tomcat启动就要初始化servlet-->
    <load-on-startup>1</load-on-startup>
</servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
<!--        匹配除jsp外的一切资源-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

7.在WEB-INF路径下创建views目录,存放jsp文件,并创建login.jsp,success.jsp,fail.jsp三个文件

 login.jsp

          <%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登录主页</title>
</head>
<body>
  <h1>欢迎登录</h1>
  <form action="login.do" method="post">
    <table>
      <tr>
      <td>账号:</td>
       <td><input type="text" name="username"></td>      
      </tr>
  <tr>
      <td>密码:</td>
       <td><input type="password" name="password"></td>      
      </tr>
      <tr>
     
      <td colspan="2" align="center"> <input type="submit" name="submit" value="登录"></td>
           
      </tr>   
          
    </table>
  </form>
</body>
</html>
View Code

success.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>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<center>
 <h2>恭喜,账户 <c:out value="${username} "></c:out>登录成功</h2> 
</center>

</body>
</html>

fail.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>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<center>
 <h2>抱歉,账户 <c:out value="${username} "></c:out>登录失败</h2> 
</center>

</body>
</html>
View Code

 8.编写业务控制器类

LoginController.java

package com.mvc.web;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController {
    
    @RequestMapping("/index")
    public String indexPage()
    {
        return "login";
    }
    
    @RequestMapping("/login")
    public String loginService(HttpServletRequest request,Model model)
    {
        String view="fail";
        try {
            request.setCharacterEncoding("utf-8");
            String username = request.getParameter("username");
            String password = request.getParameter("password");    
            
            //将账号信息存储到model,前端视图可以通过key去除对应数值
            model.addAttribute("username",username);
            
            if(username!=null&&!username.equals("")&&password!=null&&!password.equals(""))
            {
                if(username.equals(password))
                {
                    view="success";
                }
            }
            
        }catch (Exception e) {
            e.printStackTrace();
        }

        return view;
    }
    
    
}

到此一个简单的springmvc项目搭建完成,项目结构如下

 9.测试

启动tomcat服务器,在浏览器输入http://localhost:9091/springmvc3/index,访问登录界面

登录失败: 不输入任何信息提交。登录成功:账号admin,密码admin

 

posted @ 2023-04-20 11:00  YorkShare  阅读(409)  评论(0编辑  收藏  举报