适配器的主要功能是去找控制器。Action实现了什么接口
本文案例实现的功能是:在页面上输入中文名字,然后在另外一个网页上显示出来。
案例结构:
1.EmpAction代码如下,这个是控制器:
package com.guigu.shen.Action3;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;/* * * 控制器是实现Controller接口的类 * * */public class EmpAction implements Controller{ @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView modelAndView=new ModelAndView(); //设置编码方式: request.setCharacterEncoding("utf-8"); //获取员工的姓名 String username=request.getParameter("username"); System.out.println("员工的姓名"+username); //将员工的姓名封装到ModelAndView中去。 modelAndView.addObject("message", username); //将正式路径名封装到ModelAndView中。 modelAndView.setViewName("/jsp/success.jsp"); return modelAndView; }}
2.SpirngMvc_003.xml文件代码如下:
userActionID
3.springmvc.xml
4.web.xml
SpringMvc_10day_self DispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc.xml DispatcherServlet *.action index.jsp
6.index.xml
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>My JSP 'index.jsp' starting page
运行结果:正确