标签:
1. Jar 包
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.4.RELEASE</version> </dependency> </dependencies>
2. 配置 web.xml 文件
<!-- SpringMVC --> <servlet> <servlet-name>springmvc</servlet-name> <!-- 负责分发 --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/*-servlet.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>
3. 配置 springmvc-servlet.xml 在默认路径下 WEB-INF 下
<!-- 注解驱动 --> <mvc:annotation-driven/> <!-- 注解扫描包 --> <context:component-scan base-package="com.erim.web.*" /> <!-- 对加载页面的路径解析 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/"></property> <!-- 后缀 --> <property name="suffix" value=".html"></property> </bean>
4. 新建 controller 层 java 类
@Controller public class HelloWorldController { @ResponseBody @RequestMapping("/helloWorld") public String getHelloWorld() { return "Hello World ..."; } }
5. springmvc 访问静态文件
在拦截器拦截所有请求的前提下,访问静态文件也是一种请求,默认被拦截。
可以配置某些请求不被拦截,如下:springmvc-servlet.xml
<!-- 静态资源访问 --> <mvc:resources location="/img/" mapping="/img/**"/> <mvc:resources location="/js/" mapping="/js/**"/>
6. 优化
返回页面传递参数,request 中放置键值对的方式,如下:
HttpServletRequest.setAttribute("", "");
标签:
原文地址:http://www.cnblogs.com/libra0920/p/5477347.html