Spring MVC试用

简述:

试用Spring MVC, 登录控制 Login Controller


步骤:

1. 需导入要三个包


2. 新建一个LoginController类

该类的作用: 管理Login.jsp的请求,这里是给Login.jsp做定向的操作 , 最后return操作可以看到,他返回了LoginInfo.jsp

package smw.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 LoginController extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("LoginInfo.jsp");
	}

}


3. 既然是用到了spring mvc 那么需要配置一下,spring的xml

applicationContext.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	<!-- Configure the controller -->
	<bean name="/LoginController.do" class="smw.controller.LoginController"/>
</beans>

4.之后是web.xml也需要配置

web.xml

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>StudentManagementWeb</display-name>
  <welcome-file-list>
    <welcome-file>Login.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  <!-- define the name of Servlet -->
  <servlet-name>dispatcherServlet</servlet-name>
  <!-- Servlet implementation class -->
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- initialize the context -->
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <!-- load configuration -->
  <param-value>/WEB-INF/applicationContext.xml</param-value>
  </init-param>
  <!-- set loading priority -->
  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>


其中比较重要是是spring的xml文件的加载,这样才能让服务器启动的时候找到spring,从而完成spring mvc的行为


5. Login.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=gb2312">
<title>Login Page</title>
</head>
<body>
    <h1 align="center">System Login Page</h1>
	<form method="post" action="LoginController.do" class="form">
		<table width="280" border="0" align="center">
			<tr>
				<td width="87" align="center" valign="middle" >
						<div align="right">user name:</div>
				</td>
				<td width="183"><label>
						<input name="name" type="text" id="name" maxlength="10" /> </label>
				<td>
			</tr>
			<tr>
				<td height="37" align="center" valign="middle">
				  <div align="right">password: </div>
				</td>
				<td>
					<label> 
						<input name="pwd" type="password" id="pwd" maxlength="20" /> 
					</label>
				</td>
			</tr>
			<tr>
				<td align="center" valign="middle">
				  <input type="submit" name="Submit" value="submit" />
				</td>
				<td> 
					<input name="reset" type="reset" id="reset" value="reset" />
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

5. LoginInfo.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>Student Registration</title>
    <style type="text/css">
      .STYLE2 {font-size: 20pt}
    </style>
  </head>

  <body>
  	<center>
  	  <span class="STYLE2">Login info</span>
  	</center>
  	<br>
  	<table align="center" border="1">
  		<tr>
  			<td height="100"><span class="STYLE2">student name:</span></td>
  			<td height="100"><span class="STYLE2">${param.name }</span></td>
  		</tr>
  		<tr>
  			<td height="100"><span class="STYLE2">password:</span></td>
  			<td height="100"><span class="STYLE2">${param.pwd }</span></td>
  		</tr>
  		<tr>
  			<td height="100" colspan="2" align="center"><a href="Login.jsp" class="STYLE2">return to login</a></td>
  		</tr>
  </table>
  </body>
</html>


6. 整个项目包目录,


效果:

启动项目, 访问地址进入Login.jsp的页面


输入name 和 password提交之后,页面地址虽然是LoginController.do 但是 实际跑的页面时LoginInfo.jsp 原因在于LoginController类修改实际的跳转

Spring MVC试用_第1张图片


你可能感兴趣的:(Spring MVC试用)