java 在spring mvc中,如何使用@RequestMapping链接到另一个jsp?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28267233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:18:18  来源:igfitidea点击:

In spring mvc, how to link to another jsp using @RequestMapping?

javaspringjspspring-mvc

提问by Jiachang Yang

Actually, I have a welcome page(welcome.jsp). There is a link called "Contact us" in welcome page. I also have the contactinfo.jsp. I want to connect the link(Contact us) to the contactinfo.jsp. How to make it works?

实际上,我有一个欢迎页面(welcome.jsp)。欢迎页面中有一个名为“联系我们”的链接。我也有contactinfo.jsp。我想将链接(联系我们)连接到contactinfo.jsp。如何使它起作用?

I've tried, but it doesn't work. The welcome page is working, but I click the link "Contact us", its not going to contactinfo.jsp. See below is welcome controller:

我试过了,但它不起作用。欢迎页面正在运行,但我单击了“联系我们”链接,它不会转到contactinfo.jsp。请参阅下面的欢迎控制器:

@RequestMapping("/")
public String welcome(Model model) {

    model.addAttribute("greeting", "Welcome to Luke's Book Store!");
    model.addAttribute("tagline", "The one and only amazing web store");

    return "welcome";
}

@RequestMapping("/contactinfo")
public String contactinfo() {

    return "contactinfo";
}

This is my dispatcherServlet:

这是我的 dispatcherServlet:

    <mvc:annotation-driven enable-matrix-variables="true"/>

<context:component-scan base-package="com.jiachangyang.ebookstore" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id= "messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages"/> 
</bean>

<mvc:default-servlet-handler />
<mvc:resources mapping="/css/**" location="/WEB-INF/css/" />

This is the link in "welcome.jsp":

这是“welcome.jsp”中的链接:

<a href="contactinfo">Contact us</a>

回答by Jiachang Yang

Ok, I find out the answer is <a href="contactinfo">Contact us</a>

好的,我发现答案是 <a href="contactinfo">Contact us</a>

@RequestMapping("/contactinfo")
public String contactinfo() {

    return "contactinfo";
}

回答by CE ZHANG

Try this:

试试这个:

<a href="location='contactinfo'">Contact us</a>