springmvc 的面试题,表示没搞定,求解惑!


去面试,技术官给出一道上机题,没搞定,回来后,写了个demo 还是没搞定,求解
结构是这样的:

整体结构

这个结构是面试原型和我做了一部分,要求通过z:ukey 自定义标签访问Controller,没搞定,请高手告知还应该做什么?
接下来贴代码=>

Urikey.java:

   
  package com.zz.annotation;
  
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Documented
@Inherited
@Retention(RUNTIME)
@Target(ElementType.METHOD)
public @interface Urikey {
String key();
}

DemoController.java:

   
  package com.zz.controller;
  
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zz.annotation.Urikey;

@Controller
@RequestMapping(value="/demoController")
public class DemoController {
@RequestMapping(value="/getInfo",produces="text/plain;charset=UTF-8")
@Urikey(key = "demoController_getInfo")
@ResponseBody
public String getInfo(){
System.out.println("controller work !");
return "hello";
}
}

UriConverter.java:

   
  package com.zz.process;
  
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class UriConverter extends TagSupport {
private static final long serialVersionUID = -3643439400542511406L;
private String uriKey;
private String context;
public String getKey() {
return uriKey;
}
public void setKey(String uriKey) {
this.uriKey = uriKey;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
@Override
public int doStartTag() throws JspException {

return EVAL_PAGE;
}
}

web.xml:

   
  <?xml version="1.0" encoding="UTF-8"?>
  
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<jsp-config>
<taglib>
<taglib-uri>http://www.zz.com/tags</taglib-uri>
<taglib-location>/WEB-INF/zz-web.tld</taglib-location>
</taglib>
</jsp-config>

</web-app>

zz-web.tld:

   
  <?xml version="1.0" encoding="UTF-8" ?>
  

<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>ZDS Uri Converter</description>
<display-name>ZDS Uri</display-name>
<tlib-version>1.0</tlib-version>
<short-name>z</short-name>
<uri>http://www.zz.com/tags</uri>
<tag>
<description>获取URL</description>
<name>ukey</name>
<tag-class>com.zz.process.UriConverter</tag-class>
<body-content>JSP</body-content>
<attribute>
<description> resolve a key to uri</description>
<name>key</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<description>uri context</description>
<name>context</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>

页面:

请输入图片描述

获取到 Controller中的“hello”



相关链接

springMVC java

Ko君D日常 9 years, 6 months ago

你的JSP自定义标签类可以从org.springframework.web.servlet.tags.RequestContextAwareTag继承,使用
getRequestContext().getWebApplicationContext()可以得到spring的context

VA看爱我 answered 9 years, 6 months ago

Your Answer