天天看點

基于XML配置的Spring MVC(所需jar包,web.xml配置,處理器配置,視圖解析器配置)

1、添加jar

基于XML配置的Spring MVC(所需jar包,web.xml配置,處理器配置,視圖解析器配置)

2、web.xml配置

<?xml version="1.0" encoding="utf-8"?>

<web-app version="2.5"

 <!-- 配置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>

  <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

 <!-- 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 class="org.springframework.web.servlet.mvc.support.controllerclassnamehandlermapping">

  <property name="order" value="1"></property>

 <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 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>

 <!-- 配置視圖解析器 -->

 <bean class="org.springframework.web.servlet.view.internalresourceviewresolver">

  <!-- 字首 -->

  <property name="prefix" value="/web-inf/jsps/"></property>

  <!-- 字尾 -->

  <property name="suffix" value=".jsp"></property>

</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,代碼如下:

import org.springframework.validation.bindexception;

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");

 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

    <title> 'commandview.jsp'</title>

    this is commandview.jsp<br>

    name:${user.name }<br>

    address:${user.address }<br>

    age:${user.age }<br>

    tel:${user.tel }

9、myformcontroller

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

public class myformcontroller extends simpleformcontroller {

 public myformcontroller() {

 protected void dosubmitaction(object command) throws exception {

  system.out.println(u.getname());

  system.out.println("dosubmitaction");

  super.dosubmitaction(command);

對應的userform.jsp

<%

string path = request.getcontextpath();

string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";

%>

    <title> 'userform.jsp'</title>

    <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>

對應的success.jsp

    <title> 'success.jsp'</title>

     this is success.jsp!

10、mywizardformcontroller的代碼如下:

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

public class mywizardformcontroller extends abstractwizardformcontroller {

 public mywizardformcontroller(){

 protected modelandview processcancel(httpservletrequest request,

  return new modelandview("helloworld");         

//跳轉到helloworld.jsp

 protected modelandview processfinish(httpservletrequest request,

  user u = (user) command;

  system.out.println(u);

  return new modelandview("helloworld");     //跳轉到helloworld.jsp

相應的web-inf/wizard/1.jsp

    <title> '1.jsp'</title>

    <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="下一步"/>    //必須有下劃線,且值是确定的

相應的web-inf/wizard/2.jsp

    <title> '2.jsp'</title>

     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="下一步"/>    

相應的web-inf/wizard/3.jsp

    <title> '3.jsp'</title>

     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="完成"/>