<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./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:p="http://www./schema/p" xmlns:context="http://www./schema/context" xmlns:mvc="http://www./schema/mvc" xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans-4.2.xsd http://www./schema/mvc http://www./schema/mvc/spring-mvc-4.2.xsd http://www./schema/context http://www./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/下面找
|
|