打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
jsp中script引入url项目名${appName}或${app}等

<script src="/${appName}/commons/jslib/CommonValue.js"></script>

新建一个com.autumn.servlet.Dispatcher.java文件

package com.autumn.servlet;import javax.servlet.ServletConfig;import javax.servlet.ServletException;/** * Created by Administrator on 2018/6/6. */public class Dispatcher extends org.springframework.web.servlet.DispatcherServlet {    private static final long serialVersionUID = -7677752525845571027L;    @Override    public void init(ServletConfig config) throws ServletException {        super.init(config);        String appName = config.getInitParameter("appName").trim();     //web.xml中初始化参数        config.getServletContext().setAttribute("appName", appName);   //将这个appName放入servletContext中    }}

 

    <!-- springmvc的前端控制器 -->    <servlet>        <servlet-name>bookkeep-web</servlet-name>        <servlet-class>com.autumn.servlet.Dispatcher</servlet-class>   <!--原来为org.springframework.web.servlet.DispatcherServlet-->        <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:spring/Springmvc.xml</param-value>        </init-param>        <init-param>            <param-name>appName</param-name>            <param-value>Bookkeeping</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>bookkeep-web</servlet-name>        <!-- 拦截所有请求jsp除外 -->        <url-pattern>/</url-pattern>    </servlet-mapping>

Springmvc.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:p="http://www.springframework.org/schema/p"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">    <!-- 扫描controller驱动 -->    <context:component-scan base-package="com.autumn.controller" />    <!-- 注解驱动 -->    <mvc:annotation-driven />    <!--controller返回的视图解析器,例如返回login,实际解析为http://ip:port/projectName/WEB-INF/jsp/login.jsp-->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/jsp/" />        <property name="suffix" value=".jsp" />    </bean></beans>

Controler测试

@Controller@RequestMapping("/loginController")public class LoginController {    @Autowired    public LoginService loginService;    @RequestMapping("/login/{id}")    @ResponseBody   //将返回的对象解析成json字符串    public Account login(@PathVariable String id){        Account account = loginService.login(id);        return account;   //返回json字符串    }    @RequestMapping("/loginpage/{id}")    public String loginpage(@PathVariable String id){        Account account = loginService.login(id);        if (account==null) {            return "login";   //返回springmvc中配置的/WEB-INF/jsp/login.jsp        }else {            return "index";    //返回springmvc中配置的/WEB-INF/jsp/index.jsp        }    }}

其他方法

不用统一管理的${appName}可以用<base href="">标签

<base> 标签为页面上的所有链接规定默认地址或默认目标。通常情况下,浏览器会从当前文档的 URL 中提取相应的元素来填写相对 URL 中的空白。

jsp中先声明schema://server:port/contextpath/

<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>

然后在页面上声明base标签,指定base的url

<base href="<%=basePath%>">

这样的话页面中所有的图片或这里链接都会在schema://server:port/contextpath/下面找

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
SpringMVC 基础教程 简单入门实例
史上最全最强SpringMVC详细示例实战教程
Spring MVC3.0.5搭建全程
Springmvc
SpringMVC框架介绍
CGB-SRPING-MVC-V1.0
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服