SpringMVC环境搭建

淡淡的烟草味﹌ 2022-05-08 09:52 334阅读 0赞

使用注解

1 导入jar

2 在wel.xml中配置前端控制器

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="4.0"
  3. xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  6. http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
  7. <servlet>
  8. <servlet-name>jqk</servlet-name>
  9. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10. <!-- 修改配置文件路径和名称 -->
  11. <init-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>classpath:springmvc.xml</param-value>
  14. </init-param>
  15. <load-on-startup>1</load-on-startup>
  16. </servlet>
  17. <servlet-mapping>
  18. <servlet-name>jqk</servlet-name>
  19. <url-pattern>/</url-pattern>
  20. </servlet-mapping>
  21. </web-app>

如果不配置,会寻找/WEB-INF/-servlet.xml文件

3 在src下新建springmvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  12. <!-- 扫描注解 -->
  13. <context:component-scan base-package="com.lee.controller"></context:component-scan>
  14. <!-- 注解驱动 -->
  15. <!-- 包含了 DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter-->
  16. <mvc:annotation-driven></mvc:annotation-driven>
  17. <!-- 静态资源 -->
  18. <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
  19. <mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
  20. <mvc:resources location="/image/" mapping="/image/**"></mvc:resources>
  21. <mvc:resources location="/WEB-INF/js/" mapping="/abc/**"></mvc:resources>
  22. </beans>

4 编写控制器类

  1. @Controller
  2. public class DemoController {
  3. @RequestMapping("demo")
  4. public String demo() {
  5. System.out.println("执行demo");
  6. return "main.jsp";
  7. }
  8. @RequestMapping("demo2")
  9. public String demo2() {
  10. System.out.println("执行demo");
  11. return "main2.jsp";
  12. }
  13. }

不使用注解

springmvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <bean id="demo123" class="com.lee.controller.DemoController"></bean>
  10. <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  11. <property name="urlMap">
  12. <map>
  13. <!-- key解析出的是访问控制器的逻辑名 -->
  14. <entry key="demo" value-ref="demo123"></entry>
  15. </map>
  16. </property>
  17. </bean>
  18. <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
  19. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  20. <property name="prefix" value=""></property>
  21. <property name="suffix" value=".jsp"></property>
  22. </bean>
  23. </beans>

控制器类

  1. public class DemoController implements Controller{
  2. @Override
  3. public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
  4. System.out.println("执行了springmvc控制器");
  5. ModelAndView mav = new ModelAndView("main");//默认为转发跳转
  6. return mav;
  7. }
  8. }

发表评论

表情:
评论列表 (有 0 条评论,334人围观)

还没有评论,来说两句吧...

相关阅读

    相关 springMVC环境

    前言:作为一位码农,总的给这世界留下点东西吧,左思右想,只能留下点代码了,因为除了代码几乎也没有什么东西可以留下,于是编写了第一篇博客。下面进入正题springmvc环境搭建。