基于XML配置的Spring MVC(一)

2014-11-23 21:25:30 · 作者: · 浏览: 35

1、添加jar

\

2、web.xml配置


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



action
org.springframework.web.servlet.DispatcherServlet


contextConfigLocation
classpath:action-servlet.xml



action
*.do



index. jsp


3、配置action-servlet.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:m vc="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 ">










homeController //通过这个配置,对应id是homeController的可以通过这四个地址访问。
homeController
homeController
homeController


















name必须是successView,这里对应的是success.jsp
formView必须是userForm,这里对应的是userForm.jsp







wizard/1 对应的是wizard中的1.jsp
wizard/2 2.jsp
wizard/3 3.jsp












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>

'helloworld.jsp'




This is helloworld.jsp

${requestScope.msg}


7、编写MyCommandController,代码如下:

package cn.itcast.springmvc.co