在eclipse中创建springMVC项目

发布时间 2023-04-20 11:08:42作者: YorkShare

1.创建一个Java web项目

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

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

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

 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