spring整合springmvc

本文详细介绍了一个使用Spring MVC框架的应用案例,包括项目结构搭建、配置文件详解、控制器编写、服务层实现及视图展示等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、项目结构


二、导包

源码中有这些包


三、spring-mvc.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" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
  9. <!-- 配置自动扫描包 -->
  10. <context:component-scan base-package="com.ssh" />
  11. <mvc:annotation-driven />
  12. <!-- 配置视图解析器 -->
  13. <bean
  14. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  15. <property name="prefix" value="/pages/"></property>
  16. <property name="suffix" value=".jsp"></property>
  17. </bean>
  18. </beans>

四、applicationContext.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" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
  7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
  8. <!-- 配置自动扫描的包 -->
  9. <context:component-scan base-package="com.ssh">
  10. <!-- 扫描时跳过 @Controller 注解的JAVA类(控制器) -->
  11. <context:exclude-filter type="annotation"
  12. expression="org.springframework.stereotype.Controller" />
  13. </context:component-scan>
  14. </beans>

五、web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. id="WebApp_ID" version="3.0">
  6. <display-name>ssh</display-name>
  7. <welcome-file-list>
  8. <welcome-file>index.jsp</welcome-file>
  9. </welcome-file-list>
  10. <!-- 配置spring ioc容器 -->
  11. <context-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>classpath:config/applicationContext.xml</param-value>
  14. </context-param>
  15. <!-- Bootstraps the root web application context before servlet initialization -->
  16. <listener>
  17. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  18. </listener>
  19. <!-- 配置springmvc 的DispatcherServlet -->
  20. <servlet>
  21. <servlet-name>dispatcherServlet</servlet-name>
  22. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  23. <init-param>
  24. <param-name>contextConfigLocation</param-name>
  25. <param-value>classpath:config/spring-mvc.xml</param-value>
  26. </init-param>
  27. <load-on-startup>1</load-on-startup>
  28. </servlet>
  29. <!-- Map all requests to the DispatcherServlet for handling -->
  30. <servlet-mapping>
  31. <servlet-name>dispatcherServlet</servlet-name>
  32. <url-pattern>/</url-pattern>
  33. </servlet-mapping>
  34. <filter>
  35. <filter-name>CharacterEncodingFilter</filter-name>
  36. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  37. <init-param>
  38. <param-name>encoding</param-name>
  39. <param-value>UTF-8</param-value>
  40. </init-param>
  41. </filter>
  42. <filter-mapping>
  43. <filter-name>CharacterEncodingFilter</filter-name>
  44. <url-pattern>/*</url-pattern>
  45. </filter-mapping>
  46. </web-app>

六、UserController

  1. package com.ssh.controller;
  2. import javax.servlet.http.HttpServletRequest;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import com.ssh.service.UserService;
  7. @Controller
  8. @RequestMapping("/user")
  9. public class UserController {
  10. @Autowired
  11. private UserService userService;
  12. @RequestMapping(value = "save")
  13. public String save(HttpServletRequest request) {
  14. String username = request.getParameter("username");
  15. String password = request.getParameter("password");
  16. System.out.println(username + " " + password);
  17. System.out.println("controller save...");
  18. userService.save();
  19. return "success";
  20. }
  21. }

七、UserService

  1. package com.ssh.service;
  2. public interface UserService {
  3. void save();
  4. }

八、UserServiceImpl

  1. package com.ssh.service;
  2. import org.springframework.stereotype.Service;
  3. @Service
  4. public class UserServiceImpl implements UserService {
  5. @Override
  6. public void save() {
  7. System.out.println("userservice save...");
  8. }
  9. }

九、add.jsp

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <h1>添加用户</h1>
  11. <form action="user/save" method="post">
  12. 用户 <input type="text" name="username" /><br /> 密码 <input type="text"
  13. name="password" /><br /> <input type="submit" value="添加">
  14. </form>
  15. </body>
  16. </html>
  17. </html>

十、success.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. success
  11. </body>
  12. </html>

十一、运行结果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值