1,处理器的第二种配置方式
<!--配置handler --> <bean id="/FirstController" class="com.songyan.controller.FirstController"></bean> <bean id="logoon" class="com.songyan.controller.LoginHandler"> </bean> <!--映射器 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <!--映射器2 --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="login1">logoon</prop> </props> </property> </bean> <!--适配器 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> <!--适配器2 --> <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" /> <!--解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean>
2,LoginHandler
package com.songyan.controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.HttpRequestHandler; public class LoginHandler implements HttpRequestHandler { public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username=request.getParameter("username"); String password=request.getParameter("password"); request.setAttribute("username", username); request.setAttribute("password", password); request.getRequestDispatcher("login.jsp").forward(request, response); } }
3,login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘login.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="login1" method="post"> 用户名:<input type="text" name="username" >${username }<br><br> 密码:<input type="text" name="password">${password}<br><br> <input type="submit" value="login"> </form> </body> </html>
4,流程
(1)在浏览器输入要访问的URL(login.jsp)--->页面跳转到login.jsp页面
(2)在login.jsp 页面填充数据后,提交-->通过Action的参数设置(login1)
(3)提交的请求被前端控制器拦截,通过设置的配置文件的位置找到springmvc-servlet.xml文件
(4)前端控制器会调用处理器映射器,根据action中的参数找到login1(与prop 中的key对应)
(5)根据匹配的key值对应value(这里就是指的logoon)
(6)再根据value值找到对应的Handler
(7)调用handler,由于handler中的跳转语句,跳转到login.jsp页面
原文地址:https://www.cnblogs.com/excellencesy/p/9182142.html
时间: 2024-11-12 06:22:14