标签:
适配器的主要功能是去找控制器。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文件代码如下:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" > <!-- 控制器(程序员)(必须配置) --> <bean id="userActionID" class="com.guigu.shen.Action3.EmpAction"></bean> <!-- 映射器 与之前的BeanNameUrlHanderMapping不同这次采用的是SimpleUrlHandlerMapping映射器。 --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <!-- 依次写四个请求。并把之前的 <bean id="userActionID" class="com.guigu.shen.Action2.UserAction"></bean> 的id写入 --> <prop key="/showuser.action">userActionID</prop> </props> </property> </bean> <!-- 如果Action汇总书写的是视图逻辑名称,那么视图解析器就必须配置(解释一下什么是视图逻辑名称:就是类似Struts2中的,"success") 如果Action中配置的是视图真实名称,那么视图解析器就可选配置(解释一下什么是视图真实名称,就是"/jsp/success.jsp") --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> </bean> </beans>
3.springmvc.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" > <import resource="com/guigu/shen/Action3/springmvc_003.xml"/> </beans>
4.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" id="WebApp_ID" version="2.5"> <display-name>SpringMvc_10day_self</display-name> <servlet> <!--这个名字可以随便取得,但是这个名字取了之后,以后在 WEB-INF下面创建SpirngMVC的配置文件是,命名必须以这个开头, 所以这里取名叫做DispatcherServlet,那么之后的xml文件取名必须为DispatcherServlet-servlet.xml(一个字都不能差) --> <servlet-name>DispatcherServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 通知DispatcherServlet去指定目录下找到springmvc.xml配置文件 --> <!-- 注意这里的 <param-name>contextConfigLocation</param-name>一个字母都不能有错 一旦有错就会去WEB-INF下面去找 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
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+"/"; %> <!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="${pageContext.request.contextPath}/showuser.action" method="post"> 用户的姓名:<input type="text" name="username" > <input type="submit" value="提交"> </form> </body> </html>
运行结果:正确
标签:
原文地址:http://www.cnblogs.com/shenxiaoquan/p/5746489.html