基于XML配置的Spring MVC(所需jar包,web.xml配置,处理器配置,视图解析器配置)

简介: 1、添加jar 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"

1、添加jar

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>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

目录
相关文章
|
29天前
|
Java 数据库连接 数据库
hibernate正向生成数据库表以及配置——Teacher.hbm.xml
hibernate正向生成数据库表以及配置——Teacher.hbm.xml
13 1
|
5天前
|
JSON Java fastjson
Spring Boot 底层级探索系列 04 - Web 开发(2)
Spring Boot 底层级探索系列 04 - Web 开发(2)
15 0
|
20天前
|
前端开发 安全 Java
使用Java Web框架:Spring MVC的全面指南
【4月更文挑战第3天】Spring MVC是Spring框架的一部分,用于构建高效、模块化的Web应用。它基于MVC模式,支持多种视图技术。核心概念包括DispatcherServlet(前端控制器)、HandlerMapping(请求映射)、Controller(处理请求)、ViewResolver(视图解析)和ModelAndView(模型和视图容器)。开发流程涉及配置DispatcherServlet、定义Controller、创建View、处理数据、绑定模型和异常处理。
使用Java Web框架:Spring MVC的全面指南
|
29天前
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
15 1
|
1月前
|
数据库
最全三大框架整合(使用映射)——struts.xml和web.xml配置
最全三大框架整合(使用映射)——数据库资源文件jdbc.properties
10 0
|
1月前
最全三大框架整合(使用映射)——applicationContext.xml里面的配置
最全三大框架整合(使用映射)——applicationContext.xml里面的配置
7 0
|
1月前
|
存储 设计模式 前端开发
请解释 Web 应用程序的 MVC(模型-视图-控制器)架构。
【2月更文挑战第26天】【2月更文挑战第89篇】请解释 Web 应用程序的 MVC(模型-视图-控制器)架构。
|
1月前
|
XML Java Apache
Apache Flink自定义 logback xml配置
Apache Flink自定义 logback xml配置
150 0
|
1月前
|
Java 应用服务中间件
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
解决tomcat启动报错:无法在web.xml或使用此应用程序部署的jar文件中解析绝对的url [http:java.sun.com/jsp/jstl/core]
121 1
|
2月前
|
Java
logback配置,命名为logback-spring.xml
logback配置,命名为logback-spring.xml

推荐镜像

更多