springMVC

一、项目结构

二、导包

源码中有这些包

三、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. <!-- 配置自动扫描包 -->

  11. <context:component-scan base-package="com.ssh" />

  12. <mvc:annotation-driven />

  13.  
  14. <!-- 配置视图解析器 -->

  15. <bean

  16. class="org.springframework.web.servlet.view.InternalResourceViewResolver">

  17. <property name="prefix" value="/pages/"></property>

  18. <property name="suffix" value=".jsp"></property>

  19. </bean>

  20. </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. <!-- 配置自动扫描的包 -->

  10. <context:component-scan base-package="com.ssh">

  11. <!-- 扫描时跳过 @Controller 注解的JAVA类(控制器) -->

  12. <context:exclude-filter type="annotation"

  13. expression="org.springframework.stereotype.Controller" />

  14. </context:component-scan>

  15. </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.  
  11. <!-- 配置spring ioc容器 -->

  12. <context-param>

  13. <param-name>contextConfigLocation</param-name>

  14. <param-value>classpath:config/applicationContext.xml</param-value>

  15. </context-param>

  16.  
  17. <!-- Bootstraps the root web application context before servlet initialization -->

  18. <listener>

  19. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  20. </listener>

  21.  
  22. <!-- 配置springmvc 的DispatcherServlet -->

  23. <servlet>

  24. <servlet-name>dispatcherServlet</servlet-name>

  25. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  26. <init-param>

  27. <param-name>contextConfigLocation</param-name>

  28. <param-value>classpath:config/spring-mvc.xml</param-value>

  29. </init-param>

  30. <load-on-startup>1</load-on-startup>

  31. </servlet>

  32.  
  33. <!-- Map all requests to the DispatcherServlet for handling -->

  34. <servlet-mapping>

  35. <servlet-name>dispatcherServlet</servlet-name>

  36. <url-pattern>/</url-pattern>

  37. </servlet-mapping>

  38.  
  39. <filter>

  40. <filter-name>CharacterEncodingFilter</filter-name>

  41. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

  42. <init-param>

  43. <param-name>encoding</param-name>

  44. <param-value>UTF-8</param-value>

  45. </init-param>

  46. </filter>

  47. <filter-mapping>

  48. <filter-name>CharacterEncodingFilter</filter-name>

  49. <url-pattern>/*</url-pattern>

  50. </filter-mapping>

  51. </web-app>

 

六、UserController

 

  1. package com.ssh.controller;

  2.  
  3. import javax.servlet.http.HttpServletRequest;

  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;

  6. import org.springframework.stereotype.Controller;

  7. import org.springframework.web.bind.annotation.RequestMapping;

  8.  
  9. import com.ssh.service.UserService;

  10.  
  11. @Controller

  12. @RequestMapping("/user")

  13. public class UserController {

  14.  
  15. @Autowired

  16. private UserService userService;

  17.  
  18. @RequestMapping(value = "save")

  19. public String save(HttpServletRequest request) {

  20. String username = request.getParameter("username");

  21. String password = request.getParameter("password");

  22. System.out.println(username + " " + password);

  23. System.out.println("controller save...");

  24.  
  25. userService.save();

  26. return "success";

  27. }

  28. }

  • 1

 

七、UserService

 

 

 
  1. package com.ssh.service;

  2.  
  3. public interface UserService {

  4. void save();

  5. }

  • 1

 

八、UserServiceImpl

 
  1. package com.ssh.service;

  2.  
  3. import org.springframework.stereotype.Service;

  4.  
  5. @Service

  6. public class UserServiceImpl implements UserService {

  7.  
  8. @Override

  9. public void save() {

  10. System.out.println("userservice save...");

  11. }

  12. }

  • 1

九、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.  
  16. </body>

  17. </html>

  18. </html>

  • 1

 

十、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>

  • 1

十一、运行结果

 

 

--------------------- 本文来自 切格瓦拉_周 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/WEIwei1996_06/article/details/82793352?utm_source=copy

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值