前端jsp页面script引入url项目名使用${appName}

《前端jsp页面script引入url项目名使用${appName}》

新建一个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中
    }
}

 

    
    <servlet>
        <servlet-name>bookkeep-webservlet-name>
        <servlet-class>com.autumn.servlet.Dispatcherservlet-class>   
        
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring/Springmvc.xmlparam-value>
        init-param>
        <init-param>
            <param-name>appNameparam-name>
            <param-value>Bookkeepingparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>bookkeep-webservlet-name>
        
        <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">
    
    <context:component-scan base-package="com.autumn.controller" />
    
    <mvc:annotation-driven />

    
    <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}可以用标签

标签为页面上的所有链接规定默认地址或默认目标。通常情况下,浏览器会从当前文档的 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/下面找

 

点赞

Leave a Reply

Your email address will not be published. Required fields are marked *