web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>userloginspringmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>userloginspringmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
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=UTF-8">
<title>用户登录</title>
</head>
<body>
<center>
<form id="form1" name="form1" method="post" action="userLogin">
<table width="344" height="95" border="0" cellpadding="0" cellspacing="1" style="background-color: #3A8ECD; color: #000;">
<tr>
<td height="30" colspan="2" align="center" valign="middle" bgcolor="#FFFFFF">用户登录</td>
</tr>
<tr>
<td width="114" height="30" align="right" valign="middle" bgcolor="#FFFFFF">登录名:</td>
<td width="227" height="20" align="left" valign="middle" bgcolor="#FFFFFF">
<input type="text" name="username" /></td>
</tr>
<tr>
<td height="30" align="right" valign="middle" bgcolor="#FFFFFF">密码:</td>
<td height="20" align="left" valign="middle" bgcolor="#FFFFFF"><label for="textfield2"></label>
<input type="password" name="pwd" /></td>
</tr>
<tr>
<td height="30" colspan="2" align="center" valign="middle" bgcolor="#FFFFFF"><input type="submit" name="button" id="button" value="提交" />
<input type="reset" name="button2" id="button2" value="重置" /></td>
</tr>
</table>
</form>
</center>
</body>
</html>
userController.java
package com.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class UserController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
mav.addObject("username",username);
mav.addObject("pwd", pwd);
mav.setViewName("/result.jsp");
System.out.println("kasjdfkadsjf");
return mav;
}
}
springmvc_config.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-4.3.xsd">
<bean name="/userLogin" class="com.controller.UserController"/>
<!-- 处理器映射器,将处理器Handle的name作为url进行查找 -->
<bean class=
"org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- 处理器适配器,配置对处理器中handleRequest()方法的调用-->
<bean class=
"org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- 视图解析器 -->
<bean class=
"org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>
</beans>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
登录名:${username}<br/>
密码:${pwd}
</body>