Spring MVC 总结

一、组成结构:

1.web.xml // 总体配置文件

2.***-servlet.xml // 默认文档的命名格式,***为web中定义的名字

3.MyController.java // 控制器,一般继承Controller接口或者SimpleFormController类

4.myView.jsp // 显示页面

5.index.jsp //发出请求的页面

二、注意之处

1.如果myController.java继承SimpleFormController类。

应该覆盖protected ModelAndView onSubmit(Object cmd) throws Exception

当请求页面向服务器提交请求时,

(例如:

<form method="post" action="list.do"> <input type="text" name="name"> <input type="text" name="passworld"> <input type="submit" value="确定"> </form>

服务器自动调用这个函数,并把数据放入cmd中.

三、代码示例

1.web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee "> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>

2.dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/test.do">testController</prop> </props> </property> </bean> <bean id="testController" class="server.TestController"> <property name="commandClass" value="data.UserInfo"></property> <property name="successView" value="myMusicList"></property> <property name="formView" value="getMusicListFail"></property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>

3.TestController.java

package server;

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

import java.util.HashMap; import java.util.LinkedList;

import data.UserInfo; public class TestController extends SimpleFormController { protected ModelAndView onSubmit(Object cmd) throws Exception{ System.out.print("??????????????"); String now= (new java.util.Date()).toString(); HashMap map = new HashMap(); map.put("now", now); return new ModelAndView(this.getSuccessView(),map); } } 还要加一个数据类,将表单数据放入其中

UserInfo.java

package data;

public class UserInfo { private String name; private String passworld; public UserInfo(){ } public UserInfo(String name, String passworld){ this.name = name; this.passworld = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassworld() { return passworld; } public void setPassworld(String passworld) { this.passworld = passworld; } public boolean equals(Object userInfo){ if(((UserInfo)userInfo).getName().equals(this.name) && ((UserInfo)userInfo).getPassworld().equals(this.passworld)){ return true; } return false; } } 4.成功将调用的页面

myMusicList.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'hello.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->

</head> <body> 音乐列表 <br> ${now} </body> </html> 不成功返回的页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> failed </body> </html>

5.index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>第一个 Spring MVC 实例</title> </head>

<body> <form method="post" action="list.do"> <input type="text" name="name"> <input type="text" name="passworld"> <input type="submit" value="确定"> </form> </body> </html>

你可能感兴趣的:(java,spring,jsp,mvc,servlet)