基于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"
?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(所需jar包,web.xml配置,处理器配置,视图解析器配置)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mule ESB-3.Build a w
- 下一篇: 汽车分为哪几种?