helloWorld.jsp如下
<%@ page contentType="text/html; charset=GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Spring输出HelloWorld
<%
String str=(String)request.getAttribute("helloWorld");
%>
size='22'<%=str%>>
dispatcherServlet-servlet如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/ dtd/spring-beans.dtd">
<!--定义映射-->
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
helloWorldController
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
org.springframework.web.servlet.view.InternalResourceView
<bean id="helloWorldController" class="com.myHelloWorld.action.HelloWorldController">
<property name="helloWorld">
<value>HelloWorld</value>
</property>
<property name="viewPage">
<value>/jsp/helloWorld.jsp</value>
</property>
</bean>
web.xml如下
<?xml version="1.0" encoding="UTF-8"?>
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">
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
<!--初始化参数-->
contextConfigLocation
/WEB-INF/dispatcherServlet-servlet.xml
1
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
HelloWorldController.java如下
//******* HelloWorldController.java**************
package com.myHelloWorld.action;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
//实现了Controller接口
public class HelloWorldController implements Controller {
private String helloWorld;
private String viewPage;
//实现Controller接口中的handleRequest()方法
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Map model = new HashMap();
model.put("helloWorld", getHelloWorld());
return new ModelAndView(getViewPage(), model);
}
//依赖注入要返回的页面
public void setViewPage(String viewPage) {
this.viewPage = viewPage;
}
//获取要返回的页面
public String getViewPage() {
return viewPage;
}
//依赖注入显示在页面中的文字
public void setHelloWorld(String helloWorld) {
this.helloWorld = helloWorld;
}
//获取要显示在页面中的文字
public String getHelloWorld() {
return helloWorld;
}
}
本地运行http://localhost:8080/myHelloWorld/helloWorld.do报如下错误:
HTTP Status 404 - /myHelloWorld/jsp/helloWorld.jsp
type Status report
message /myHelloWorld/jsp/helloWorld.jsp
description The requested resource is not available.
Apache Tomcat/7.0.55