初学者对于Spring框架的难度:引用Jar包不全,或者不正确;
1、运行界面
2、客户端页面
index.jsp 的代码
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Spring 3.0 MVC demo</title>
</head> <body>
<a href="hello.html">Say Hello1</a>
<a href="hello2.html">Say Hello2</a>
<a href="1/hello.html">Say Hello3</a>
</body>
</html>
/jsp/hello.jsp的代码
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>link</title>
</head> <body>
This is Hello!<br>
${message}
</body>
</html>
3、配置文件代码:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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"> <display-name>spring3MVC</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
spring-servlet.xml 的代码
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="net.spring.controller" /> <bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
4、后台Java文件代码 HelloWorldController.java
package net.spring.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
public class HelloWorldController {
@RequestMapping("/hello2")
public ModelAndView helloWorld() { String message = "1";
System.out.println(message);
return new ModelAndView("hello", "message", message); }
@RequestMapping("/hello.html")
public ModelAndView helloWorld2() { String message = "2";
System.out.println(message);
return new ModelAndView("hello", "message", message); }
@RequestMapping("/1/hello.html")
public ModelAndView helloWorld3() { String message = "3!";
System.out.println(message);
return new ModelAndView("hello", "message", message); }
}
5、引用类库
至此可以运行程序,但是Log的Jar包引用不全,会有警告。