基于XML配置的Spring MVC

1、添加jar

基于XML配置的Spring MVC

2、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>
  <servlet-name>action</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 通过初始化参数指定配置文件的位置 -->
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:action-servlet.xml</param-value>
  </init-param>
 </servlet>
 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
3、配置action-servlet.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:mvc="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-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 ">
 
 <!-- bean名url处理器映射 默认-->
 <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
  <property name="order" value="3"></property>
 </bean>
 
 <!-- 简单url处理器映射 -->
 <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
   <props>
    <prop key="/home.do">homeController</prop>     //通过这个配置,对应id是homeController的可以通过这四个地址访问。
    <prop key="/a.do">homeController</prop>
    <prop key="/b.do">homeController</prop>
    <prop key="/c.do">homeController</prop>
   </props>
  </property>
  <property name="order" value="2"></property>
 </bean>
 
 <!-- 控制器类名处理器映射 -->
 <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
  <property name="order" value="1"></property>
 </bean>
 
 <!-- 自定义控制器 http://localhost/SpringMVC_01/home.do-->
 <bean id="homeController" name="/hello.do" class="cn.itcast.springmvc.controller.HomeController"></bean>
 
 <!-- 命令控制器 -->
 <bean name="/command.do" class="cn.itcast.springmvc.controller.MyCommandController"></bean>
 
 <!-- 表单控制器 -->
 <bean name="/form.do" class="cn.itcast.springmvc.controller.MyFormController">
  <property name="successView" value="success"></property>       name必须是successView,这里对应的是success.jsp
  <property name="formView" value="userForm"></property>            formView必须是userForm,这里对应的是userForm.jsp
 </bean>
 
 <!-- 向导表单控制器 -->
 <bean name="/wizard.do" class="cn.itcast.springmvc.controller.MyWizardFormController">
  <property name="pages">
   <!-- 逻辑名 -->
   <list>
    <value>wizard/1</value>     对应的是wizard中的1.jsp
    <value>wizard/2</value>                                         2.jsp
    <value>wizard/3</value>                                         3.jsp
   </list>
  </property>
 </bean>
 
 <!-- 配置视图解析器 -->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <!-- 前缀 -->
  <property name="prefix" value="/WEB-INF/jsps/"></property>
  <!-- 后缀 -->
  <property name="suffix" value=".jsp"></property>
 </bean>
</beans>
4、编写实体bean:

package cn.itcast.springmvc.domain;

public class User {
 private String name;
 private String address;
 private Integer age;
 private String tel;
 
 /**
  * @return the name
  */
 public String getName() {
  return name;
 }
 
 /**
  * @param name the name to set
  */
 public void setName(String name) {
  this.name = name;
 }
 
 /**
  * @return the address
  */
 public String getAddress() {
  return address;
 }
 /**
  * @param address the address to set
  */
 public void setAddress(String address) {
  this.address = address;
 }
 
 /**
  * @return the age
  */
 public Integer getAge() {
  return age;
 }
 
 /**
  * @param age the age to set
  */
 public void setAge(Integer age) {
  this.age = age;
 }
 
 /**
  * @return the tel
  */
 public String getTel() {
  return tel;
 }
 
 /**
  * @param tel the tel to set
  */
 public void setTel(String tel) {
  this.tel = tel;
 }
}

5、编写HomeController,代码如下:

package cn.itcast.springmvc.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HomeController extends AbstractController {

 @Override
 protected ModelAndView handleRequestInternal(HttpServletRequest req,
   HttpServletResponse resp) throws Exception {
  String name = req.getParameter("name");
  String msg = "hello " + name + " !";
  System.out.println("HomeController...");
  return new ModelAndView("helloworld","msg",msg);
 }

}

6、HomeController返回到的页面未:helloworld.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>
  <head>
    <title> ‘helloworld.jsp‘</title>
  
  </head>
 
  <body>
    This is helloworld.jsp<br>
    ${requestScope.msg}
  </body>
</html>

7、编写MyCommandController,代码如下:

package cn.itcast.springmvc.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

import cn.itcast.springmvc.domain.User;

/**
 *命令控制器
 */
public class MyCommandController extends AbstractCommandController {
 
 public MyCommandController(){
  //注册命令类
  this.setCommandClass(User.class);
  //命令名称
  this.setCommandName("user");
 }

 @Override
 protected ModelAndView handle(HttpServletRequest request,
   HttpServletResponse response, Object command, BindException errors)
   throws Exception {
  User u = (User)command;
  System.out.println("name:" + u.getName() + " address:" + u.getAddress());
  return new ModelAndView("commandView","user",u);
 }

}

8、commandView.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>
  <head>
    <title> ‘commandView.jsp‘</title>
  
  </head>
 
  <body>
    This is commandView.jsp<br>
    name:${user.name }<br>
    address:${user.address }<br>
    age:${user.age }<br>
    tel:${user.tel }
  </body>
</html>

9、MyFormController

package cn.itcast.springmvc.controller;

import org.springframework.web.servlet.mvc.SimpleFormController;

import cn.itcast.springmvc.domain.User;

public class MyFormController extends SimpleFormController {

 public MyFormController() {
  this.setCommandClass(User.class);
  this.setCommandName("user");
 }

 @Override
 protected void doSubmitAction(Object command) throws Exception {
  User u = (User)command;
  System.out.println(u.getName());
  System.out.println("doSubmitAction");
  super.doSubmitAction(command);
 }

}

对应的userForm.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
  <head>
    <title> ‘userForm.jsp‘</title>
  
  </head>
 
  <body>
    <form action="<%=path%>/form.do" method="post">
     name:<input type="text" name="name"><br>
     age:<input type="text" name="age"><br>
     address:<input type="text" name="address"><br>
     tel:<input type="text" name="tel"><br>
     <input type="submit" value="submit"/>
    </form>
  </body>
</html>

对应的success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>
  <head>
    <title> ‘success.jsp‘</title>
  </head>
 
  <body>
     This is success.jsp!
  </body>
</html>

10、MyWizardFormController的代码如下:

package cn.itcast.springmvc.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractWizardFormController;

import cn.itcast.springmvc.domain.User;

public class MyWizardFormController extends AbstractWizardFormController {
 
 public MyWizardFormController(){
  this.setCommandClass(User.class);
  this.setCommandName("user");

 }

 @Override
 protected ModelAndView processCancel(HttpServletRequest request,
   HttpServletResponse response, Object command, BindException errors)
   throws Exception {
  return new ModelAndView("helloworld");          //跳转到helloworld.jsp
 }
 
 @Override
 protected ModelAndView processFinish(HttpServletRequest request,
   HttpServletResponse response, Object command, BindException errors)
   throws Exception {
  User u = (User) command;
  System.out.println(u);
  return new ModelAndView("helloworld");     //跳转到helloworld.jsp
 }

}

相应的WEB-INF/wizard/1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
    <title> ‘1.jsp‘</title>
  
  </head>
 
  <body>
    <form action="<%=path %>/wizard.do" method="post">
     name:<input type="text" name="name" value="${requestScope.user.name}"><br>            //标出的颜色区域可以用于回显
     <input type="submit" name="_cancel" value="取消"/>        //必须有下划线,且值是确定的
     <input type="submit" name="_target1" value="下一步"/>    //必须有下划线,且值是确定的
    </form>
  </body>
</html>

相应的WEB-INF/wizard/2.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
    <title> ‘2.jsp‘</title>
  
  </head>
 
  <body>
    <form action="<%=path %>/wizard.do" method="post">
     address:<input type="text" name="address" value="${requestScope.user.address }"><br>
     <input type="submit" name="_target0" value="上一步"/>    //表示跳转到最开始的页面。
     <input type="submit" name="_cancel" value="取消"/>
     <input type="submit" name="_target2" value="下一步"/>    
    </form>
  </body>
</html>

相应的WEB-INF/wizard/3.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
    <title> ‘3.jsp‘</title>
  
  </head>
 
  <body>
    <form action="<%=path %>/wizard.do" method="post">
     age:<input type="text" name="age" value="${requestScope.user.age }"><br>
     tel:<input type="text" name="tel" value="${requestScope.user.tel }"><br>
     <input type="submit" name="_target1" value="上一步"/>
     <input type="submit" name="_cancel" value="取消"/>
     <input type="submit" name="_finish" value="完成"/>
    </form>
  </body>
</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

基于XML配置的Spring MVC,布布扣,bubuko.com

基于XML配置的Spring MVC

上一篇:自动化测试 selenium中WebDriver 和WebElement的区别


下一篇:Python3.2官方文档-日志和弱引用