springMVC之HelloWorld

一、总结

  1.web项目一定要把引用的jar包放在WEB-INF/lib下(common-logging1.2,spring4.1.6所有包,其实不需要那么多,懒得筛选了,)

  2.web.xml中要初始化servlet配置文件,如*-servlet.xml

  3.显示声明处理器映射为org.springframework.web.servlet.handler.SimpleUrlHandlerMapping,默认的BeanNameUrlMapping尝试未成功。

  

二、环境环境

  1.我的开发环境(windows+EclipseJee+tomcat6+jdk6),不同的同学可以忽略

  2.项目路径图:

    springMVC之HelloWorld

三、代码:

  web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springmvcDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list> <!-- 本配置文件并没有指定dd-servlet.xml,这是因为DispatcherServlet会自动查找WEB-INF/<servlet-name>-servlet.xml的配置文件 -->
<servlet>
<servlet-name>servletName</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 一定要初始化servlet配置文件,不然会报错:Allocate exception for servlet servletName -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/zz/config/servletName-servlet.xml</param-value>
<!-- <param-value>/WEB-INF/servletName-servlet.xml</param-value> -->
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>servletName</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

  

servletName-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springmvcDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list> <!-- 本配置文件并没有指定dd-servlet.xml,这是因为DispatcherServlet会自动查找WEB-INF/<servlet-name>-servlet.xml的配置文件 -->
<servlet>
<servlet-name>servletName</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 一定要初始化servlet配置文件,不然会报错:Allocate exception for servlet servletName -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/zz/config/servletName-servlet.xml</param-value>
<!-- <param-value>/WEB-INF/servletName-servlet.xml</param-value> -->
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>servletName</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

IndexController.java

package zz.springmvc.demo;

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 IndexController implements Controller{ // 扩展属性,通过mv—servlet设值注入
public String word; public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
} @Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
System.out.println("lalalala");
return new ModelAndView("index","dataview",word);
} }

index.jsp

<%@ 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>
${dataview}
</body>
</html>

首页:index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home Page</title>
</head>
<body>
WelCome
</body>
</html>

    

上一篇:在项目中 background transiton 带来的"便利"与“坑”


下一篇:【SSH网上商城项目实战18】过滤器实现购物登录功能的判断