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>