springmvc入门登录功能

学习springmvc的时候的一个入门功能,登录功能。配置好web框架,导入需要springjar包和springmvc需要的两个jar包,就可以编码了,首先写了登录需要的jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="login">
  邮箱:<input type="text" name="email"><br>
  密码:<input type="password" name="password"><br>
  <input type="submit" value="登录">
</form>
</body>
</html>

和登录成功的页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>login_ok</title>
</head>
<body>
login_ok
</body>
</html>

还需要建立一个spring的xml applicationContent-mvc.xml,因为要通过注释的方式使一个普通的java类变成一个Handler处理器,所以要在这个xml配置扫描包。这里我遇到的问题就是idea自动提示的扫描包的配置文件的头部声明。结果不对,就运行失败了。

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="com.ysbt.web"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
<!--    配置前端控制器、中央控制器、分发控制器,用户的请求都会经过它的处理-->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--        配置属性contextConfigLocation,指定DispatcherServlet去操作的spring配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContent-mvc.xml</param-value>
        </init-param>
<!--        项目启动时自动加载DispatcherServlet-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

处理器,通过包扫描@Controller把这个类当做处理器。login方法返回的值要与返回到的页面名称一致。

package com.ysbt.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserServlet {
    @RequestMapping(value = "login")//@RequestMapping("login")同理
    public String login(){
        return "login_ok";
    }
}

image

画一下springmvc的执行流程图

image

注意

若没有在DispatcherServlet中指定applicationContext-mvc.xml。则默认会去/WEB-INF/springDispatcherServlet-servlet.xml找配置文件。(web.xml配置里DispatcherServlet的servlet-name加上-servlet)
DispatcherServlet父类FrameworkServlet中属性
public static final String DEFAULT_NAMESPACE_SUFFIX = "-servlet";的注释如下

Suffix for WebApplicationContext namespaces. If a servlet of this class is given the name "test" in a context, the namespace used by the servlet will resolve to "test-servlet".
posted @ 2024-03-13 13:22  尤所不同  阅读(32)  评论(0编辑  收藏  举报