第一步:建立一個web項目html
第二步:導包,SpringMVC和Freemarket包pan.baidu.com/s/1kT5cnpdjava
第三步配置的web.xmlweb
3.1spring
/ /配置spring配置文件路徑express
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/applicationContext.xml,
classpath*:/applicationContext-*.xml
</param-value>
</context-param>
spring-mvc
3.2mvc
/ /配置SpringMVC中的servletapp
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
ui
3.3url
/ /配置加載彈簧配置文件容器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
第四步
ApplicationContext-mvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"
default-lazy-init="true">
< - !註解,而且啓動掃描掃描的路徑org.walden.testmvc包下面的類 - >
<context:component-scan base-package="org.walden.testmvc" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</beans>
applicationContext.xml中
<?xml version="1.0" encoding="UTF-8"?>
ControllerMVCTest.java類
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ControllerMVCTest {
@RequestMapping(value="/welcome",method={RequestMethod.GET})
public ModelAndView getFirstPage(HttpServletRequest request){
ModelAndView mvc = new ModelAndView("test");
mvc.addObject("name", "walden");
return mvc;
}
}
test.ftl模版內容:
<!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> Hello ${name} </body> </html>