标签:
1.导Jar包
2.配置前端控制器(web.xml)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Spring_mvc</display-name> <!-- springmvc前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- 指定加载的springmvc框架配置文件 默认命名方式:servlet的name+‘-servlet.xml‘,即springmvc-servlet.xml,此文件默认在/WEB-INF/下 --> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!-- post乱码过虑器 --> <filter> <filter-name>CharacterEncodingFilter</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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
3.配置处理器映射器(springmvc.xml)
<!--
将bean的name当成action的url
-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
4、配置处理器适配置器
<!-- 处理器适配置器
凡时实现controller接口的类都当成SimpleControllerHandlerAdapter执行的对象
-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
5、编写handler即后端控制器
public class HelloWorld implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView modelAndView = new ModelAndView(); String message = "hello world!!!"; modelAndView.addObject("message", message); modelAndView.setViewName("helloworld"); return modelAndView; } }
7、配置视图解析器
在springmvc.xml中配置
<!-- 视图解析
解析jsp视图,默认支持jstl标签
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>
8、编写action并配置
<!-- hello World的action
这个action的url通过name配置,url为hello.action
-->
<bean name="/hello.action" class="cn.springmvc.action.HelloWorld"/>
标签:
原文地址:http://www.cnblogs.com/jsnan/p/4609615.html