1、创建如下项目结构
2、在src下的com.springmvc下创建User.java
package com.springmvc; public class User {
private String uname; public String getUname() {
return uname;
} public void setUname(String uname) {
this.uname = uname;
} @Override
public String toString() {
return "User [uname=" + uname + "]";
} }
User.java
3、在src下的com.springmvc下创建MVCController.java
package com.springmvc; import java.io.IOException; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; /**
* @Controller 标注控制类,负责注册一个bean到spring上下文中
* @RequestMapping 注解为控制器指定可以处理哪些 URL 请求
* http://MVCController/hello
*
*/
@Controller
public class MVCController {
/*所有方法的return 返回的是页面视图的名称*/ //1.自动匹配参数
@RequestMapping("/hello.do")
public String hello(HttpServletRequest request,HttpServletResponse response) throws IOException{
//跳转页面
return "/index.jsp";
}
//2.自动匹配参数,传递一个参数
@RequestMapping("/login.do")
public void login(HttpServletResponse response,String uname) throws IOException{
//login的页面,uname是表单的name属性名
System.out.println("登录通过String参数接用户名:"+uname);
//跳转方式1:
response.sendRedirect("index.jsp");
}
//2.自动匹配参数
@RequestMapping("/register.do")
public void test(HttpServletResponse response,User user) throws IOException{
//register页面的name属性名只要和User类的属性名保持一致,这里参数拿对象接就好,
//这里register页面的name属性名中不用写user.属性名,不能像stuts2注入对象一样
System.out.println("登录通过User对象参数接用户名:"+user);
//跳转方式1:
response.sendRedirect("index.jsp");
} }
MVCController.java
4、在WebRoot下的WEB-INF下创建springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
<!-- 1.配置要自动扫描的包 -->
<context:component-scan base-package="com.springmvc"/> <!-- 2.支持spingmmvc注解,自动匹配 -->
<mvc:annotation-driven/> <!-- 3.如果当前请求为“/”时,则转发到“mvc/hello” 也就是默认首启项的设置,web.xml欢迎页面节点可以啥也不写-->
<!-- <mvc:view-controller path="/" view-name="forward:/mvc/hello"/> --> </beans>
springmvc-servlet.xml
5、编辑WebRoot下的WEB-INF下的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<!-- SpringMVC的配置 -->
<servlet>
<servlet-name>springmvc</servlet-name> <!-- DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,
Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据相应的规则分发到目标Controller来处理, -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
</web-app>
web.xml
6、在WebRoot下创建login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'index.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="login.do" method="post">
user:<input type="text" name="uname"/>
<input type="submit" value="ok"/>
</form>
</body>
</html>
login.jsp
7、在WebRoot下创建register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'index.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="register.do" method="post">
user:<input type="text" name="uname"/>
<input type="submit" value="ok"/>
</form>
</body>
</html>
register.jsp
8、在WebRoot下创建index.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>
操作成功!
</body>
</html>
index.jsp
9、运行效果如下
(1)直接通过浏览器访问hello.do
(2)登录时,后台用String参数名接值
(3)注册时,后台用User对象参数接值