SpringMVC的HelloWord,springmvchelloword

來源:互聯網
上載者:User

SpringMVC的HelloWord,springmvchelloword
SpringMVC的(helloWord)1、匯入jar包2、建立spring和springMVC的xml檔案3、在web.xml中配置spring的監聽器和核心servlet和位置 下面詳細說每個步驟1、建立一個web工程demoapp2、在web-INF/lib下匯入spring的jar  3、spring的xml,只需要寫一個helloword的話只需要配置一個掃描包就可以啦(spring-config.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:aop="http://www.springframework.org/schema/aop"       xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:task="http://www.springframework.org/schema/task"        xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"        xsi:schemaLocation="       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd       ">        <!-- 配置掃描包 -->      <context:annotation-config />      <context:component-scan base-package = "com.demo"/><!--</beans> 4、管理springmvc的xml(application.xml) <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:util="http://www.springframework.org/schema/util"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:oxm="http://www.springframework.org/schema/oxm"    xsi:schemaLocation="        http://www.springframework.org/schema/util        http://www.springframework.org/schema/util/spring-util-4.1.xsd        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-4.1.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd        http://www.springframework.org/schema/oxm            http://www.springframework.org/schema/oxm/spring-oxm.xsd       " >    <!-- 掃描包 -->  <context:component-scan base-package ="com.demo.**.controller" >       <context:include-filter type = "annotation" expression ="org.springframework.stereotype.Controller" />  </context:component-scan >   <!-- 預設的視圖解析器- -->    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver" >        <property name="prefix" value="/WEB-INF/jsp/"/>        <property name="suffix" value=".jsp"/>    </bean> </beans>  3、在web.xml中配置spring <!-- Spring設定檔 start  -->      <context-param>            <param-name>contextConfigLocation</param-name>             <param-value>classpath:spring-*.xml</param-value>            <!-- <param-value>classpath:spring-config.xml;classpath:spring-redis.xml;</param-value> -->      </context-param>       <listener>            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      </listener>       <servlet>            <servlet-name>spring</servlet-name>            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>            <init-param>                  <param-name>contextConfigLocation</param-name>                  <param-value>classpath:application.xml</param-value>            </init-param>      </servlet>      <servlet-mapping>            <servlet-name>spring</servlet-name>            <url-pattern>/</url-pattern>      </servlet-mapping>       <!-- Spring設定檔 end  -->  spring的監聽器--->context|LoaderListener,核心的servlet-->dispatcherServlet 5、建立一個類,TestController,在對應的方法上面加上@requestMapping(“路徑”)     必須建立在controller包下面,因為在application.xml中配置的掃描包  @Controllerpublic class TestController {       @RequestMapping("/hello")      public String hello(){            System.out.println("hello this is hello method");            return "hello";      }} 

 

  6、在頁面上使用相應的路徑就可以啦  <a href="<%=path %>/hello">hello springMVC</a>SpringMVC的(helloWord)1、匯入jar包2、建立spring和springMVC的xml檔案3、在web.xml中配置spring的監聽器和核心servlet和位置 下面詳細說每個步驟1、建立一個web工程demoapp2、在web-INF/lib下匯入spring的jar  3、spring的xml,只需要寫一個helloword的話只需要配置一個掃描包就可以啦(spring-config.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:aop="http://www.springframework.org/schema/aop"       xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:task="http://www.springframework.org/schema/task"        xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"        xsi:schemaLocation="       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd       ">        <!-- 配置掃描包 -->      <context:annotation-config />      <context:component-scan base-package = "com.demo"/><!--</beans> 4、管理springmvc的xml(application.xml) <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:util="http://www.springframework.org/schema/util"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:oxm="http://www.springframework.org/schema/oxm"    xsi:schemaLocation="        http://www.springframework.org/schema/util        http://www.springframework.org/schema/util/spring-util-4.1.xsd        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-4.1.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd        http://www.springframework.org/schema/oxm            http://www.springframework.org/schema/oxm/spring-oxm.xsd       " >    <!-- 掃描包 -->  <context:component-scan base-package ="com.demo.**.controller" >       <context:include-filter type = "annotation" expression ="org.springframework.stereotype.Controller" />  </context:component-scan >   <!-- 預設的視圖解析器- -->    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver" >        <property name="prefix" value="/WEB-INF/jsp/"/>        <property name="suffix" value=".jsp"/>    </bean> </beans>  3、在web.xml中配置spring <!-- Spring設定檔 start  -->      <context-param>            <param-name>contextConfigLocation</param-name>             <param-value>classpath:spring-*.xml</param-value>            <!-- <param-value>classpath:spring-config.xml;classpath:spring-redis.xml;</param-value> -->      </context-param>       <listener>            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      </listener>       <servlet>            <servlet-name>spring</servlet-name>            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>            <init-param>                  <param-name>contextConfigLocation</param-name>                  <param-value>classpath:application.xml</param-value>            </init-param>      </servlet>      <servlet-mapping>            <servlet-name>spring</servlet-name>            <url-pattern>/</url-pattern>      </servlet-mapping>       <!-- Spring設定檔 end  -->  spring的監聽器--->context|LoaderListener,核心的servlet-->dispatcherServlet 5、建立一個類,TestController,在對應的方法上面加上@requestMapping(“路徑”)     必須建立在controller包下面,因為在application.xml中配置的掃描包  @Controllerpublic class TestController {       @RequestMapping("/hello")      public String hello(){            System.out.println("hello this is hello method");            return "hello";      }}   6、在頁面上使用相應的路徑就可以啦  <a href="<%=path %>/hello">hello springMVC</a>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.