访问Page服务端方法

         <!-- 注释 -->
        <!-- 在开发中使用较多的情况, 在客户端调用当前页面的服务端方法. -->               
        <!-- 设置ScriptManager控件的EnablePageMethods属性为:True, 并且在需要使用后台方法前加属性标记[WebMethod] -->
        <!-- 服务端方法必须为静态的, 需要客户端调用的才需要设置为静态的; -->
        <!-- 注意在服务端类中要打开命名空间using System.Web.Services, WebMethod类在此命名空间中-->

  1.   <script language="javascript" type="text/javascript">
  2.    function ExecuteServerMethod(value)
  3.    {
  4.     PageMethods.ReturnStringServerMethod(value,CallBackResult);
  5.    }
  6.    
  7.    function CallBackResult(result)
  8.    {
  9.     alert(result);
  10.    }
  11.   </script>
  1.     [WebMethod]
  2.     public static string ReturnStringServerMethod(string str)
  3.     {        
  4.         return "Hello " + str;
  5.     }
### Thymeleaf 服务端渲染实现方式 #### Spring Boot 集成 Thymeleaf 进行服务端渲染 在Spring Boot项目中集成Thymeleaf可以简化配置并提高开发效率[^2]。为了实现在服务端进行页面渲染,开发者需遵循特定的设置流程。 首先,在`pom.xml`文件里加入依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 接着创建控制器类来处理HTTP请求并将模型数据传递给视图层: ```java import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HelloController { @GetMapping("/hello") public String hello(Model model){ model.addAttribute("message", "Hello, world!"); return "hello"; } } ``` 上述代码定义了一个简单的GET映射路径/hello,当访问该URL时会触发此方法执行,并向名为“hello”的HTML模板传入消息参数。 最后编写对应的Thymeleaf HTML模板文件位于resources/templates目录下(例如:hello.html),利用`${}`表达式获取Java对象属性值显示于前端界面上: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head lang="en"> <meta charset="UTF-8"/> <title>Hello Page</title> </head> <body> <h1 th:text="${message}">Default Message</h1> </body> </html> ``` 这种模式允许服务器预先计算好所有的动态内容再发送完整的HTML文档至客户端浏览器展示出来,不仅有助于提升用户体验还便于搜索引擎优化(SEO)[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值