SpringMVC的HelloWord
SpringMVC的(helloWord)
1、导入jar包
2、创建spring和springMVC的xml文件
3、在web.xml中配置spring的监听器和核心servlet和位置
下面详细说每个步骤
1、新建一个web工程demoapp
2、在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中配置的扫描包
@Controller
public 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工程demoapp
2、在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中配置的扫描包
@Controller
public 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>