一:
运行效果:
点击提交之后显示效果
二:
(1).web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <!-- 解决post请求中文乱码问题 --> <filter> <filter-name>CharaterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>endcoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharaterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <!-- 配置前端过滤器 --> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 表示容器再启动的时候立即记载Servlet --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
(2)springmvc-servlet.xml(都是第二种的方法:标有数字2的)
<?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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" > <!-- 配置处理器Handle--> <bean name="/firstController" class="com.inspur.controller.FirstController"> </bean> <!-- Handler2 --> <bean id="login" class="com.inspur.controller.LoginController"></bean> <!-- 处理映射器,将处理器Handle的name作为url进行查找 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- Handler处理方法2 --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/login1">login</prop> </props> </property> </bean> <!-- 处理适配器2 --> <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean> <!-- 视图解析器 --> <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> </beans>
(3)LoginController.java
package com.inspur.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 LoginController implements HttpRequestHandler { @Override public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String username = request.getParameter("username"); String password = request.getParameter("password"); request.setAttribute("username", username); request.setAttribute("password", password); //请求成功,登陆login.jsp页面,获取uername和password的值 request.getRequestDispatcher("/login.jsp").forward(request, response); } }
(4)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> 密码:<input type="text" name="password">${password }<br> <input type="submit" value="提交"> </form> </body> </html>
三:运行流程
1.输入jsp页面的路径:http://localhost:8080/SpringMVCTest01/login.jsp
2.web.xml前端控制器DispacherServlet会拦截浏览器
3.调用spring-servlet.xml,查找login。这时候找
4.prop的login会找到对应id为login的bean
5.调用bean里面的LoginController控制器
原文地址:https://www.cnblogs.com/sunxiaoyan/p/9182148.html
时间: 2024-10-31 01:20:36