SpringMVC实现操作的第二种方式
一:
运行效果:
点击提交之后显示效果
二:
(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控制器
SpringMVC实现操作的第二种方式的更多相关文章
- SpringMVC 返回json的两种方式
前后台数据交互使用json是一种很重要的方式.本文主要探讨SpringMVC框架使用json传输的技术. 请注意,本文所提到的项目使用Spring 版本是4.1.7,其他版本在具体使用上可能有不一样的 ...
- springMVC返回数据的四种方式
转自:https://blog.csdn.net/itcats_cn/article/details/82119673 springMVC返回数据的四种方式:第一种,通过request.setAttr ...
- Java创建线程的第二种方式:实现runable接口
/*需求:简单的卖票程序多个窗口买票 创建线程的第二种方式:实现runable接口 *//*步骤1.定义类实现Runable接口2.覆盖Runable接口中的run方法 将线程要运行的代码存放在 ...
- IOS文件操作的两种方式:NSFileManager操作和流操作
1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...
- JS定义函数的2种方式以及区别简述(为什么推荐第二种方式)
无意中看到了阮一峰大神多年前的一篇博客: 12种不宜使用的Javascript语法 看到第9条的时候受到了启发,感觉之前没怎么理解清楚的一些问题好像突然就清晰了,如下图 可能光这样看,有些小伙 ...
- 利用spring实现服务启动就自动执行某些操作的2种方式
第一种方式,用bean的init-method属性 <bean class="com.emax.paycenter.log.LogBridge" init-method=&q ...
- Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)
1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整 ...
- SpringMVC实现文件下载的两种方式及多文件下载
1.传统方法 @RequestMapping("/download") public String download( String fileName ,String filePa ...
- SpringMVC获取参数的几种方式
前言: 年末了,忙了一年了却发现系统的整理的东西很少,一些基础的东西都未做整理,这里就将它随便整理一下,增加一些印象,当然在网上看到一些好的资料也会整理下来以备后用.今天整理一下springMVC获取 ...
随机推荐
- SLF4J 与Log4J
为什么要使用SLF4J而不是Log4J 每一个Java程序员都知道日志对于任何一个Java应用程序,尤其是服务端程序是至关重要的,而很多程序员也已经熟悉各种不同的日志库如java.util.loggi ...
- 通过7zip压缩备份文件bat
for %%X in (*log20*) do "c:\Program Files\7-Zip\7z.exe" a "backups\%%X.zip" &quo ...
- 知问前端——日历UI(二)
datapicker外观选项 属性 默认值/类型 说明 disabled false/布尔值 禁用日历 numberOfMonths 1/数值 日历中同时显示的月份个数.默认为1,如果设置3就同时显示 ...
- jQuery操纵DOM
一.基本操作 1.html() - 类似于原生DOM的innerHTML属性 *获取 - html(); *设置 - html("html代码"); 2.val() - 类似于原生 ...
- bzoj 1064 图论
我们根据能否看见建图,有向图边权设成1,然后我们转成无向图, 对于每条有向边连一条反边,边权是-1,然后从每个块中任意一个点开始 dfs,每个点有一个值,经过一条边到另一个点之后,用原来的点值和边权 ...
- UVA 10303 How Many Trees? (catlan)
刚开始没看出时卡特兰数列.直接套高精度版 #include <map> #include <set> #include <list> #include <cm ...
- OC的UUID生成
NSString *uuidStr = [[NSUUID UUID] UUIDString];
- 【 Linux 】为lnmp架构添加memcached支持
一.首先搭建lnmp平台,这里不再演示.通过php页面来进行测试如下: [root@node1 ~]# vim /usr/local/nginx/html/info.php <?php $lin ...
- 【 Keepalived 】Nginx or Http 主-主模式
上一篇:[ Keepalived ]Nginx or Http 主-备模式 在此基础上进行修改并实现 Keepalived主-主模式 首先,需要理解的是:主-备模式是一个VIP在工作,主-主模式则需要 ...
- Python与Mysql交互
#转载请联系 在写内容之前,先放一张图,bling- 这张图算是比较详细的表达出了web开发都需要什么.用户访问网页,就是访问服务器的网页文件.这些网页文件由前端工程师编写的.服务器通常用nginx/ ...