Controller包
package Controller;
/**
* 1.属性名字要和请求参数名字一致。
*
*/
public class Admin {
private String adminCode;
private String pwd;
public String getAdminCode() {
return adminCode;
}
public void setAdminCode(String adminCode) {
this.adminCode = adminCode;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
------------------------------------------------HelloController----------------------------------------------------
package Controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* 基于注解的mvc应用 处理器类
*
* @author Ward
*
*/
@Controller
public class HelloController {
@RequestMapping("/hello.do")
public String hello() {
System.out.println("HelloController的hello方法");
return "hello";//返回视图名字
}
@RequestMapping("/toLogin.do")
public String Login() {
System.out.println("调用了Login的方法");
return "login";
}
@RequestMapping("/login.do")
public String checkLogin(HttpServletRequest request) {
/**
*
* 获取请求参数值,这是第一种方式
* 前端控制器调用处理器之前,利用反射分析这个方法有哪些参数,
* 分析完后发现有一个请求对象,把请求对象作为参数传进来,
* ,
*/
System.out.println("HelloController的checkLogin的方法");
String adminCode =request.getParameter("adminCode");
String pwd =request.getParameter("pwd");
System.out.println("账号:"+adminCode+"密码:"+pwd);
return "index";//返回另一个视图名,及jsp
}
@RequestMapping("/login2.do")
/**
* 读取请求参数的第二种方式
* 使用请求参数名字作为方法的入参(参数名)。
* 如果参数名字不一致,可以在参数前加注解@Re'questParam(*),
* *号是哪个请求参数的名字。
* 例如(String adminCode,@Re'questParam(pwd) String pwd1)
*/
public String checkLogin2(String adminCode,String pwd) {
System.out.println("checkLogin2方法");
System.out.println("账号:"+adminCode+",密码"+pwd);
return "index";
}
@RequestMapping("/login3.do")
public String checkLogin3(Admin admin) {
/**
* 读取请求参数的第三种方式,封装成java bean的方式,属性要和请求参数名一样
* 有对应的get set方法
*/
System.out.println("checkLogin3的方法");
System.out.println("账号"+admin.getAdminCode()+"密码:"+admin.getPwd());
return "index";
}
@RequestMapping("/login4.do")
/**
* 向页面传值的第一种方式,使用request对象绑定数据
* @return
*/
public String checkLogin4(Admin admin,HttpServletRequest request) {
System.out.println("checkLogin4的方法");
System.out.println("账号:"+admin.getAdminCode());
//向页面传值
//利用请求对象绑定数据,然后转发给index.jsp,index.jsp通过请求对象获取数据
request.setAttribute("adminCode", admin.getAdminCode());
//默认情况下是转发的方式跳转到index.jsp
return "index";
}
@RequestMapping("/login5.do")
/**
* 向页面传值的第二种方式,使用ModelMap作为方法的参数
*/
public String checkLogin5(Admin admin,ModelMap data) {
System.out.println("check5的方法");
System.out.println("账号"+admin.getAdminCode());
/**
*
* 等同于request.setAttribute
*/
data.addAttribute("adminCode",admin.getAdminCode());
return "index";
}
@RequestMapping("/login6.do")
/**
* 向页面传值的第三种方式,
* 使用session绑定数据
*
* @return
*/
public String checkLogin6(Admin admin,HttpSession session) {
System.out.println("checkLogin的6的方法");
System.out.println(admin.getAdminCode());
session.setAttribute("adminCode",admin.getAdminCode());
return "index";
}
@RequestMapping("login7.do")
/*
* 向页面传值的第四种方式,
* 使用ModelAndView对象作为方法的返回值。
*/
public ModelAndView checkLogin7(Admin admin) {
System.out.println("check7的方法");
System.out.println("账号:"+admin.getAdminCode());
Map<String,Object> data=new HashMap<String,Object>();
//相当于request.setAttribute
data.put("adminCode", admin.getAdminCode());
ModelAndView mav=new ModelAndView("index",data);
return mav;
}
}
---------------------------------------------------------配置文件-------------------------------------------------------------
<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 配置组件扫描,会扫描Controller下面所有的包,子包下的所有类,当有发现类上有注解,会当成bean来管理 -->
<context:component-scan base-package="Controller"></context:component-scan>
<!--组件扫描只管4个注解(service,conponent,controller ,repostly).RequestMapping属于springmvc特定的注解,需要配置mvc注解扫描
不然RequestMapping注解就不起作用了 -->
<mvc:annotation-driven/>
<!-- 配置视图解析器 。prefix表示前缀-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
--------------------------------------------------
--------------------------------- ------- ------------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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatcherServlet的初始化方法会启动spring
容器,所以需要配置初始化参数来指定spring配置文件的位置
如果没有配置初始化参数,则会找WEB-INF/*-servlet.xml ,*与前端控制器的name一样 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
-----------------
--------------------------------------------------------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>
<title>Insert title here</title>
</head>
<body>
Login success!
<p>
adminCode:${adminCode}
</p>
</body>
</html>
-----------------------------------------------------login.jsp------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<title>Insert title here</title>
</head>
<!-- action="login4(1/2/3...)各演示一个知识点" -->
<body
style="font-size: 30px;font-tyle:italic;">
<form action="login7.do" method="post">
账号:<input name="adminCode"/><br/>
密码:<input name="pwd" type="password"/><br/>
<input type="submit" value="登录">
</form>
</body>
</html>
------------------------------------------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>first spring-mvc</title>
</head>
<body style="font-size: 30px;font-style: italic;">
Hello,SpringMVC!
</body>
</html>
--------------------------------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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatcherServlet的初始化方法会启动spring
容器,所以需要配置初始化参数来指定spring配置文件的位置
如果没有配置初始化参数,则会找WEB-INF/*-servlet.xml ,*与前端控制器的name一样 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>