spring MVC 初探 (HelloWorld)

一、使用spring MVC 須要導入相關jar包 html

二、web.xml 啓用spring MVC java

 

<servlet>
        <servlet-name>spring3mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定配置文件所在目錄src 默認目錄在/WEB-INF/ 默認文件名<servlet-name>-servlet.xml -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>
        <!-- 啓動就加載 -->
        <load-on-startup>1</load-on-startup>
    </servlet>

  <!-- 攔截全部請求 -->
    <servlet-mapping>
        <servlet-name>spring3mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

 

三、編寫Controller 使用xml配置文件方式須要繼承Controllerweb

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloWorld implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { String value = "nihao"; //返回視圖 參數1爲顯示頁面由於後面配置文件配置了後綴因此這裏指須要給出頁面名稱不須要後綴 
// 傳參使用鍵值對 參數會綁定到request域
return new ModelAndView("/test", "Key", value); } }

四、顯示頁面test.jspspring

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body>
使用EL表達式取值
Ok!!!!!!!!!!! ${key } </body>

五、spring MVC配置文件 單獨Controlle配置spring-mvc

<?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:p="http://www.springframework.org/schema/p" 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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
    <bean name="/test" class="com.ly.Controller.HelloWorld"></bean>

    <!-- 定義跳轉的文件的先後綴 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <!-- 前綴就是指定你的顯示頁面根目錄 -->
        <property name="prefix" value="/" />
        <!-- 後綴給出顯示頁面後綴 -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

六、一個controller編寫多個方法  須要繼承 MultiActionController  (一樣使用xml配置文件方式)mvc

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; public class multiController extends MultiActionController { public ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("add"); return new ModelAndView("/test", "key", "value"); } public ModelAndView update(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("update"); return new ModelAndView("/test", "key", "value"); } }

 

七、一個controller 編寫多個方法 配置文件配置app

<?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:p="http://www.springframework.org/schema/p" 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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
    <bean name="/testc" class="com.ly.Controller.multiController">
        <property name="methodNameResolver" ref="methodNameResolver"></property>
    </bean>

    <!-- 一個controller 編寫多個方法 解析 -->
    <bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
        <property name="paramName" value="action"></property>
    </bean>

    <!-- 添加註解驅動 -->
    <mvc:annotation-driven />
    <!-- 定義跳轉的文件的先後綴 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <!-- 前綴就是指定你的顯示頁面根目錄 -->
        <property name="prefix" value="/" />
        <!-- 後綴給出顯示頁面後綴 -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
相關文章
相關標籤/搜索