{"id":1259,"date":"2025-01-27T21:40:18","date_gmt":"2025-01-27T13:40:18","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1259"},"modified":"2025-01-27T21:44:12","modified_gmt":"2025-01-27T13:44:12","slug":"web%e5%bc%80%e5%8f%91%e5%9f%ba%e7%a1%80%ef%bc%9a%e4%bb%8e%e9%9b%b6%e5%bc%80%e5%a7%8b%e5%ad%a6%e4%b9%a0java-web%e5%bc%80%e5%8f%91%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/27\/web%e5%bc%80%e5%8f%91%e5%9f%ba%e7%a1%80%ef%bc%9a%e4%bb%8e%e9%9b%b6%e5%bc%80%e5%a7%8b%e5%ad%a6%e4%b9%a0java-web%e5%bc%80%e5%8f%91%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5\/","title":{"rendered":"Java Web\u5f00\u53d1\u5165\u95e8\uff1a\u4eceServlet\u5230Spring\u6846\u67b6\u7684\u5168\u9762\u5b66\u4e60"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f<strong>Web\u5f00\u53d1\u57fa\u7840<\/strong>\u7684Java\u6559\u7a0b\uff0c\u6db5\u76d6\u4e86Servlet\u4e0eJSP\u3001Spring\u6846\u67b6\u3001\u6570\u636e\u5e93\u8fde\u63a5\u3001RESTful API\u4ee5\u53ca\u524d\u7aef\u57fa\u7840\u7b49Web\u5f00\u53d1\u6838\u5fc3\u6982\u5ff5\uff0c\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u5b8c\u6574\u7684Web\u5f00\u53d1\u5165\u95e8\u6307\u5357\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Java\u6559\u7a0b\u4e4b\uff1aWeb\u5f00\u53d1\u57fa\u7840<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1. Servlet\u4e0eJSP<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Web\u5e94\u7528\u7ed3\u6784<\/strong><br>Java Web\u5e94\u7528\u4e00\u822c\u7531\u591a\u4e2a\u7ec4\u4ef6\u7ec4\u6210\uff0c\u5305\u62ec\u524d\u7aef\u9875\u9762\uff08HTML\u3001JSP\uff09\u548c\u540e\u7aef\u903b\u8f91\uff08Servlet\uff09\u3002Web\u5e94\u7528\u901a\u5e38\u4f7f\u7528Servlet\u6765\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42\uff0cJSP\u7528\u4e8e\u52a8\u6001\u751f\u6210HTML\u9875\u9762\u3002<\/li>\n\n\n\n<li><strong>Servlet\u57fa\u7840<\/strong><br>Servlet\u662fJava Web\u5f00\u53d1\u4e2d\u7684\u6838\u5fc3\u7ec4\u4ef6\u4e4b\u4e00\uff0c\u5b83\u901a\u8fc7\u5b9e\u73b0<code>HttpServlet<\/code>\u7c7b\u6765\u5904\u7406\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u3002\u4e00\u4e2a\u7b80\u5355\u7684Servlet\u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  @WebServlet(\"\/hello\")\n  public class HelloServlet extends HttpServlet {\n      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\n          response.getWriter().write(\"Hello, World!\");\n      }\n  }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>JSP\u4e0eServlet\u96c6\u6210<\/strong><br>JSP\uff08JavaServer Pages\uff09\u7528\u4e8e\u52a8\u6001\u751f\u6210HTML\u5185\u5bb9\uff0c\u901a\u5e38\u4e0eServlet\u914d\u5408\u4f7f\u7528\u3002Servlet\u5904\u7406\u8bf7\u6c42\u5e76\u5c06\u6570\u636e\u4f20\u9012\u7ed9JSP\u8fdb\u884c\u5c55\u793a\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  &lt;%-- hello.jsp --%&gt;\n  &lt;html&gt;\n      &lt;body&gt;\n          &lt;h1&gt;${message}&lt;\/h1&gt;\n      &lt;\/body&gt;\n  &lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2. Spring\u6846\u67b6<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Spring\u6982\u5ff5\u4e0e\u4f9d\u8d56\u6ce8\u5165\uff08DI\uff09<\/strong><br>Spring\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u5f00\u6e90\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u63a7\u5236\u53cd\u8f6c\uff08IoC\uff09\u548c\u4f9d\u8d56\u6ce8\u5165\uff08DI\uff09\u7684\u529f\u80fd\uff0c\u7b80\u5316\u4e86Java\u5e94\u7528\u7684\u914d\u7f6e\u548c\u5f00\u53d1\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  @Component\n  public class MyService {\n      public void serve() {\n          System.out.println(\"Service is running...\");\n      }\n  }\n\n  @Configuration\n  @ComponentScan(basePackages = \"com.example\")\n  public class AppConfig {\n      @Bean\n      public MyService myService() {\n          return new MyService();\n      }\n  }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Spring MVC\u67b6\u6784<\/strong><br>Spring MVC\u662f\u57fa\u4e8e\u6a21\u578b-\u89c6\u56fe-\u63a7\u5236\u5668\uff08MVC\uff09\u8bbe\u8ba1\u6a21\u5f0f\u7684Web\u6846\u67b6\uff0c\u5206\u4e3aModel\u3001View\u3001Controller\u4e09\u4e2a\u90e8\u5206\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  @Controller\n  public class MyController {\n      @RequestMapping(\"\/home\")\n      public String home(Model model) {\n          model.addAttribute(\"message\", \"Hello, Spring MVC!\");\n          return \"home\";\n      }\n  }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Spring Boot\u57fa\u7840<\/strong><br>Spring Boot\u7b80\u5316\u4e86Spring\u5e94\u7528\u7684\u914d\u7f6e\uff0c\u4f7f\u7528\u201c\u5f00\u7bb1\u5373\u7528\u201d\u7684\u65b9\u5f0f\u5feb\u901f\u6784\u5efaSpring\u5e94\u7528\u3002\u53ea\u9700\u4e00\u4e2a\u4e3b\u7c7b\u548c<code>@SpringBootApplication<\/code>\u6ce8\u89e3\u5373\u53ef\u542f\u52a8\u5e94\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  @SpringBootApplication\n  public class MyApp {\n      public static void main(String&#91;] args) {\n          SpringApplication.run(MyApp.class, args);\n      }\n  }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3. \u6570\u636e\u5e93\u8fde\u63a5\u4e0e\u4e8b\u52a1\u7ba1\u7406<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>JDBC\u4e0e\u6570\u636e\u5e93\u8fde\u63a5<\/strong><br>Java\u901a\u8fc7JDBC\uff08Java Database Connectivity\uff09\u6765\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u8fde\u63a5\u3002Spring\u63d0\u4f9b\u4e86\u66f4\u4fbf\u6377\u7684JDBC\u652f\u6301\uff0c\u7b80\u5316\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  @Autowired\n  private JdbcTemplate jdbcTemplate;\n\n  public void queryDatabase() {\n      String sql = \"SELECT * FROM users\";\n      List&lt;User&gt; users = jdbcTemplate.query(sql, new BeanPropertyRowMapper&lt;&gt;(User.class));\n  }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e8b\u52a1\u7ba1\u7406<\/strong><br>Spring\u63d0\u4f9b\u4e86\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u65b9\u6cd5\u4e0a\u4f7f\u7528<code>@Transactional<\/code>\u6ce8\u89e3\u6765\u7b80\u5316\u4e8b\u52a1\u5904\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  @Transactional\n  public void updateUser(User user) {\n      userRepository.save(user);\n  }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>4. RESTful API<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>RESTful\u6982\u5ff5<\/strong><br>REST\uff08Representational State Transfer\uff09\u662f\u4e00\u79cd\u57fa\u4e8eHTTP\u534f\u8bae\u7684\u67b6\u6784\u98ce\u683c\uff0cRESTful API\u901a\u8fc7HTTP\u534f\u8bae\u8fdb\u884c\u8bf7\u6c42\u548c\u54cd\u5e94\uff0c\u4f7f\u7528HTTP\u65b9\u6cd5\uff08GET\u3001POST\u3001PUT\u3001DELETE\uff09\u6765\u64cd\u4f5c\u8d44\u6e90\u3002<\/li>\n\n\n\n<li><strong>GET<\/strong>\uff1a\u83b7\u53d6\u8d44\u6e90<\/li>\n\n\n\n<li><strong>POST<\/strong>\uff1a\u521b\u5efa\u8d44\u6e90<\/li>\n\n\n\n<li><strong>PUT<\/strong>\uff1a\u66f4\u65b0\u8d44\u6e90<\/li>\n\n\n\n<li><strong>DELETE<\/strong>\uff1a\u5220\u9664\u8d44\u6e90<\/li>\n\n\n\n<li><strong>\u4f7f\u7528Spring Boot\u6784\u5efaRESTful\u670d\u52a1<\/strong><br>Spring Boot\u53ef\u4ee5\u8f7b\u677e\u5730\u6784\u5efaRESTful\u670d\u52a1\uff0c\u901a\u5e38\u901a\u8fc7<code>@RestController<\/code>\u6ce8\u89e3\u6807\u8bc6\u63a7\u5236\u5668\uff0c\u5e76\u4f7f\u7528<code>@RequestMapping<\/code>\u5b9a\u4e49\u8bf7\u6c42\u8def\u5f84\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  @RestController\n  @RequestMapping(\"\/api\/users\")\n  public class UserController {\n      @GetMapping(\"\/{id}\")\n      public User getUser(@PathVariable Long id) {\n          return userService.getUserById(id);\n      }\n\n      @PostMapping\n      public User createUser(@RequestBody User user) {\n          return userService.createUser(user);\n      }\n  }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>5. \u524d\u7aef\u57fa\u7840\u77e5\u8bc6<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>HTML\u3001CSS\u3001JavaScript\u57fa\u7840<\/strong><br>Web\u5f00\u53d1\u9700\u8981\u638c\u63e1HTML\uff08\u8d85\u6587\u672c\u6807\u8bb0\u8bed\u8a00\uff09\u3001CSS\uff08\u5c42\u53e0\u6837\u5f0f\u8868\uff09\u548cJavaScript\uff08\u524d\u7aef\u7f16\u7a0b\u8bed\u8a00\uff09\u3002\u8fd9\u4e9b\u57fa\u7840\u77e5\u8bc6\u5e2e\u52a9\u5f00\u53d1\u8005\u521b\u5efa\u7ed3\u6784\u5316\u7684\u9875\u9762\u3001\u8bbe\u8ba1\u6837\u5f0f\u5e76\u5b9e\u73b0\u4ea4\u4e92\u3002<\/li>\n\n\n\n<li><strong>\u524d\u7aef\u6846\u67b6\uff08\u5982React\u3001Vue\uff09<\/strong><br>\u4e3a\u4e86\u63d0\u9ad8\u524d\u7aef\u5f00\u53d1\u6548\u7387\uff0cReact\u548cVue\u7b49\u524d\u7aef\u6846\u67b6\u63d0\u4f9b\u4e86\u7ec4\u4ef6\u5316\u3001\u6570\u636e\u7ed1\u5b9a\u548c\u865a\u62dfDOM\u7b49\u7279\u6027\uff0c\u6781\u5927\u5730\u7b80\u5316\u4e86\u524d\u7aef\u5f00\u53d1\u3002<\/li>\n\n\n\n<li><strong>React<\/strong> <code>function Hello() { return &lt;h1>Hello, React!&lt;\/h1>; }<\/code><\/li>\n\n\n\n<li><strong>Vue<\/strong> <code>&lt;div id=\"app\"> &lt;h1>{{ message }}&lt;\/h1> &lt;\/div> &lt;script> new Vue({ el: '#app', data: { message: 'Hello, Vue!' } }); &lt;\/script><\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u90e8\u5206\u4ecb\u7ecd\u4e86Java Web\u5f00\u53d1\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u5305\u62ecServlet\u4e0eJSP\u3001Spring\u6846\u67b6\u3001RESTful API\u3001\u524d\u7aef\u57fa\u7840\u7b49\u5185\u5bb9\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u672f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u642d\u5efaWeb\u5e94\u7528\uff0c\u5e76\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u5e94\u7528Spring\u7b49\u6846\u67b6\u63d0\u5347\u5f00\u53d1\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u662fWeb\u5f00\u53d1\u57fa\u7840\u7684Java\u6559\u7a0b\uff0c\u6db5\u76d6\u4e86Servlet\u4e0eJSP\u3001Spring\u6846\u67b6\u3001\u6570\u636e\u5e93\u8fde\u63a5\u3001RESTful [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-1259","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1259","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1259"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1259\/revisions"}],"predecessor-version":[{"id":1260,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1259\/revisions\/1260"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}