Spring MVC + Jquery load

本文探讨了 Spring MVC 中路径映射的具体实现方式,特别是如何通过 @RequestMapping 注解来定义 URL 路径,并解释了路径变量 @PathVariable 的使用方法。此外,还涉及了如何通过指定路径和视图名称进行页面跳转。
@Controller
@RequestMapping("/nvg")
public class NavigationController {
private Logger logger = LoggerFactory.getLogger(getClass());


@RequestMapping("/{path}")
public ModelAndView navigation1(@PathVariable("path") String path) {
logger.debug("orgEditDialog");
ModelAndView view = new ModelAndView("org/"+path);
return view;
}
}
或者
@RequestMapping("/nvg")
public class NavigationController {
private Logger logger = LoggerFactory.getLogger(getClass());


@RequestMapping("/myPageName")//(1)
public ModelAndView navigation1(@PathVariable("path") String path) {
logger.debug("orgEditDialog");
ModelAndView view = new ModelAndView("org/myPageName");
//(2) return view;
}
}

不明白,为什么(1)跟(2)一定要一样,才能访问,否则找不到这个model。


    $("#orgHomeIncludeDiv").load("nvg/orgEditDialog", paramters, function (response, status, xhr) {
if(status!="success"&&status!="notmodified"){
$.WebUtil.error("无法获取请求的资源.","提示信息");
}
});
### 如何整合 SpringSpringMVC、MyBatis、JSP、CSS 和 jQuery #### 1. 技术栈概述 Spring 是一个轻量级的 Java 开发框架,主要用于解决企业应用开发中的复杂性问题[^1]。Spring 提供了 IoC(控制反转)和 AOP(面向切面编程),使得应用程序更加模块化和易于维护。 Spring MVCSpring 的一部分,专注于 Web 层开发,提供了一个灵活的请求处理机制[^4]。MyBatis 是一种持久层框架,用于简化 JDBC 编程,允许开发者通过 XML 或注解定义 SQL 查询语句[^5]。 JSP(JavaServer Pages)、CSS(层叠样式表)和 jQueryJavaScript 库)则分别负责前端视图渲染、页面美化和动态交互功能。 --- #### 2. 整体架构设计 项目的整体结构可以分为以下几个层次: - **表现层**:使用 JSP 结合 CSS 和 jQuery 实现用户界面。 - **控制层**:利用 Spring MVC 处理 HTTP 请求并将业务逻辑委派给服务层。 - **服务层**:封装核心业务逻辑,通常由 Spring Bean 组件实现。 - **持久层**:借助 MyBatis 访问数据库,执行 CRUD 操作。 --- #### 3. 配置文件详解 ##### (1) `applicationContext.xml` 文件 该文件主要配置 Spring 容器的核心组件,包括数据源、事务管理和 Mapper 扫描等功能[^2]。 ```xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 数据源配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- SqlSessionFactoryBean 配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:mapper/*.xml" /> </bean> <!-- Transaction Manager 配置 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 启用基于注解的事务管理 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans> ``` --- ##### (2) `spring-mvc.xml` 文件 此文件专门针对 Spring MVC 进行配置,主要包括视图解析器和静态资源映射等内容。 ```xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 扫描 Controller 包路径 --> <context:component-scan base-package="com.example.controller" /> <!-- 启用 Spring MVC 默认特性 --> <mvc:annotation-driven /> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 静态资源配置 --> <mvc:resources mapping="/css/**" location="/static/css/" /> <mvc:resources mapping="/js/**" location="/static/js/" /> </beans> ``` --- ##### (3) `web.xml` 文件 作为整个 Web 应用程序的入口点,`web.xml` 负责初始化 DispatcherServlet 并加载上下文配置文件[^4]。 ```xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 加载 Spring 上下文 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- 初始化 DispatcherServlet --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> ``` --- #### 4. 前端集成 ##### (1) 使用 JSP 渲染页面 JSP 页面可以通过 `${}` 表达式绑定后台传递的数据,并结合 HTML/CSS 构建美观的布局。 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>首页</title> <link rel="stylesheet" href="<%=request.getContextPath()%>/css/style.css"> </head> <body> <h1>Welcome to SSM Framework!</h1> <script src="<%=request.getContextPath()%>/js/jquery.min.js"></script> <script> $(document).ready(function() { alert('Page Loaded!'); }); </script> </body> </html> ``` ##### (2) 动态交互 jQuery 可以轻松实现 AJAX 请求或其他动态效果。 ```javascript $.ajax({ url: '/api/data', method: 'GET', success: function(response) { console.log(response); }, error: function(xhr, status, error) { console.error(error); } }); ``` --- #### 5. 测试与部署 完成以上步骤后,可以在本地 Tomcat 中运行项目并验证其功能是否正常工作。如果一切顺利,则可以将项目打包为 WAR 文件并部署到生产环境。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值