码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC笔记

时间:2017-08-17 18:38:47      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:jsp   path   return   ns-3   tps   display   业务   work   mil   

springmvc属于spring框架的后续产品,用在基于MVC的表现层开发,类似于struts2框架

导入springiocspringweb , springmvc相关的jar

/WEB-INF/下创建web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>springMVCone</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- 注册springmvc核心控制器 -->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 通知DispatcherServlet去指定的目录下加载springmvc.xml配置文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:DispatcherServlet-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    
</web-app>

 

创建HelloAction.java控制器类

 

/**
 * 控制器
 * @author AdminTC
 */
public class HelloAction implements Controller{
    /**
     * 业务方法
     */
    public ModelAndView handleRequest(HttpServletRequest requqest,HttpServletResponse response) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message","这是我的第一个springmvc应用程序");
        modelAndView.setViewName("/jsp/success.jsp");
        return modelAndView;
    }
}

 

DispatcherServlet-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  ">

    <bean name="/hello.action" class="a.HellowAction"></bean>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前置 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 后置 -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

 

springMVC笔记

标签:jsp   path   return   ns-3   tps   display   业务   work   mil   

原文地址:http://www.cnblogs.com/lm970585581/p/7382804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!