前端登录页面代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>登录页面</h1>
<form action="login.do" method="post">
username:<input type="text" name="username"></br>
password:<input type="password" name="password"></br>
<input type="submit">
</form>
</body>
</html>
配置springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
">
<!--开启mvc-->
<mvc:annotation-driven />
<!--开启注解依赖-->
<context:annotation-config></context:annotation-config>
<!--自动扫描包-->
<context:component-scan base-package="com.bh"></context:component-scan>
</beans>
配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 配置编码 -->
<filter>
<filter-name>characterfilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--springmvc的核心控制器-->
<servlet>
<servlet-name>disp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置初始化参数-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>disp</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
controller类
package com.bh.controller;
import com.bh.po.User;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@Controller
public class LoginController {
/*
* 方式一:原始方式
* */
/* @RequestMapping("/login")
public String login1(HttpServletRequest request, HttpServletResponse response){
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username);
System.out.println(password);
return "/result.html";
}*/
/*
* 方式二:restful方式
* */
/*@RequestMapping("/login")
public String login2(@RequestParam("username") String name,@RequestParam("password") String pwd){
System.out.println(name);
System.out.println(pwd);
return "/result.html";
}*/
/*
* 方式三:普通方式
* */
/* @RequestMapping("/login")
public String login3(String username,String password){
System.out.println(username);
System.out.println(password);
return "/result.html";
}*/
/*
* 方式四:对象方式
* */
@RequestMapping("/login")
public String login4(User user){
String username = user.getUsername();
String password = user.getPassword();
System.out.println(username);
System.out.println(password);
return "/result.html";
}
}
方式四的实体类
package com.bh.po;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}