您的位置:首页 > 编程语言 > Java开发

spring mvc构建WEB应用程序入门例子

2014-05-01 23:21 483 查看
在使用spring mvc 构建web应用程序之前,需要了解spring mvc 的请求过程是怎样的,然后记录下如何搭建一个超简单的spring mvc例子。

1) spring mvc的请求经历

请求由DispatcherServlet分配给控制器(根据处理器映射),在控制器完成处理后,请求会被发送到一个视图(根据viewController解析逻辑视图) 来呈现输出结果。

整理成下图所示:

<?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>Archetype Created Web Application</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</context-param>

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>

<error-page>
<error-code>404</error-code>
<location>/error/404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/404.html</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/error/404.html</location>
</error-page>
</web-app>View Code

③ 配置文件spring-config.xml和dispatcher-servlet.xml

spring-config.xml:

<!-- 自动检测Bean -->
<context:component-scan base-package="com.test.service.impl" />

<!-- 自动装配Bean -->
<context:annotation-config />

dispatcher-servlet.xml:

<!-- 处理静态资源 -->
<!-- <mvc:resources location="resources/*" mapping="resources/**"/> -->

<!-- 启动spring mvc注解 -->
<mvc:annotation-driven/>
<!-- 自动检测Controller类 --> <context:component-scan base-package="com.test.controller"/>
<!-- velocity 配置 -->
<bean id="velocityConfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/vm/" />
<property name="velocityProperties">
<props>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
<prop key="velocimacro.library">macro/macros.vm</prop>
</props>
</property>
</bean>

<!-- 配置视图解析器,用于解析逻辑视图名-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="cache" value="false" />
<property name="prefix" value="" />
<property name="suffix" value=".vm" />
<property name="layoutUrl" value="layout/layout.vm" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="dateToolAttribute" value="dateTool" />
</bean>

④ Controller类:

@Controller
public class HomeController {

private UserService userService;

@Autowired
public HomeController(UserService userService){
this.userService = userService;
}

//指定请求的路径
@RequestMapping("home.html")
public String showHomePage(Map<String,Object> model){
model.put("count", userService.getUsersCount());
return "home";
}
}

showHomePage对应的请求为:localhost/项目名/home.html

整个项目工程图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: