Spring温故知新:WEB篇(二)Hello,World!
在准备写代码之前,我们首先得把下列必备的库导入项目:
spring-framework-2.5.6\dist\spring.jar
spring-framework-2.5.6\dist\modules\spring-webmvc.jar
spring-framework-2.5.6\lib\jakarta-commons\commons-logging.jar
spring-framework-2.5.6\lib\j2ee\jstl.jar
只要把这几个文件拷贝到项目的WebContent\WEB-INF\lib目录下,然后在eclipse里刷新一下项目,就可以自动导入库。
这时候我们可以开始修改项目的WebContent\WEB-INF文件夹下的web.xml这个配置文件。
要使用Spring来制作一个web,首先我们得在web.xml里新建一个servlet,这个servlet的作用就是告诉tomcat容器,当一个请求符合某一种规则的时候就用Spring来负责处理这个请求。
例如我们需要让Spring来接管所有以.do为后缀的url请求,那么就需要修改web.xml为以下内容:
web.xml:
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" 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 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><display-name>SpringWeb</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping></web-app>
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"><!-- 视图 --><bean id="viewResolver"value="org.springframework.web.servlet.view.JstlView"/> --><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean><!-- 映射 --><bean id="urlMapping"value="helloWorld" /></bean></beans>
package com.iteye.bolide74.action;import java.util.HashMap;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;public class HelloWorld implements Controller {private String viewPage;public String getViewPage() {return viewPage;}public void setViewPage(String viewPage) {this.viewPage = viewPage;}@Overridepublic ModelAndView handleRequest(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception {String msg=arg0.getParameter("msgValue");msg="你输入的值为"+msg;Map model = new HashMap();model.put("msg", msg);return new ModelAndView(getViewPage(), model);}}
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!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=UTF-8"><title>Insert title here</title></head><body><form action="helloWorld.do"><input type="text" name="msgValue" value="" /> <input type="submit" value="speak" /></form><%=request.getAttribute("msg")%></body></html>