2015-05-28 20:32:14|
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="3.0" 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_3_0.xsd">
<display-name>FirstSpringMVCProject111</display-name>
<servlet> // 要特别注意 servlet的配置,这是必不可少的,而且是个 联动关系。 这里,声明了一个 servlet,名字是: spring-dispatcher. 所以,相对应的,应该创建一个 spring-dispatcher-servlet.xml 文件。
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
// servlet-name 与 servlet-mapping,总是成对地出现。</web-app>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.uu.hellocontroller" /> //这个package 要特别注意, 这是 controller 所在的 package 名称。 所有的controller 都应该在这个 package 下。 其实,一个 controller 就是一个 class;
<mvc:annotation-driven/> // 表明支持注解模式
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
package com.uu.hellocontroller;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping("/welcome")
public ModelAndView helloWorld() {
ModelAndView model = new ModelAndView("HelloPage");
model.addObject("msg","hello world");
return model;
}
}
<html>
<body>
<h1>First Spring MVC Application Demo</h1>
<h2>${msg}</h2>
// 这 msg 就是 controller 代码中对应的 model.addObject("msg","hello world");
</body>
</html>
联系客服