Spring MVC(一)登陆界面获取用户名和密码并跳转其它页面

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u014042146/article/details/50058677

实现Spring MVC的入门,在登陆界面填写用户名和密码,提交后获取用户名和密码跳转到另一个页面

一、新建一个JAVA WEB项目,并导入Spring的相关JAR包,并在web.xml中声明拦截器DispatcherServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>springmvc01</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

二、在WebContent下创建hello.jsp页面,发出请求:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="index.do" method="post">
	用户名:<input type="text" name="username"/>
	密    码:<input type="text" name="password"> 
	<input type="submit" value="提交"/>
</form>	
</body>
</html>


三、在WebContent目录下创建文件夹view,里面用于渲染的jsp界面,并在里面新建index.jsp用于渲染:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
	<table border="1">
		<tr>
			<th>用户名</th>
			<th>密码</th>
		</tr>
		<tr>
			<td>${username }</td>
			<td>${password }</td>
		</tr>
	</table>
</body>
</html>

四、创建控制器HelloController 继承AbstractController重写handleRequestInternal:

package com.liu.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloController extends AbstractController {

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println("----:"+username);
		System.out.println("----:"+password);
		//在index.jsp页面进行渲染
		ModelAndView mav = new ModelAndView("index");
		mav.addObject("username", username);
		mav.addObject("password", password);
		return mav;
		
	}

}

五、在WEB-INF下创建Spring配置文件,命名规则是【servlet-name】-servlet.xml,所以根据web.xml中可知,我们在WEB-INF下面创建springmvc-servlet.xml配置文件,在里面配置控制器和视图解析器

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc
	                    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
			    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-3.0.xsd">

	<!-- 配置HandlerMapping  根据benaname找到对应Conntroller  -->
	<bean
		class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping">
	</bean>

	<!-- 配置Controller,在hello.jsp页面的action等于这里的name,表示交由该控制器处理 -->
	<bean  name="index.do" class="com.liu.controller.HelloController"></bean>

	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前缀 -->
		<property name="prefix" value="/view/"></property>
		<!-- 后缀 -->
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>
运行结果:



点击提交后在index.jsp进行渲染


后台并输出:



展开阅读全文

没有更多推荐了,返回首页