简体   繁体   English

Spring MVC。 请求映射不起作用。 index.jsp的链接无法加载。

[英]Spring MVC. Request mapping not working. Links from index.jsp don't load.

I have a maven project using Spring and I currently have my index page up and running on Tomcat. 我有一个使用Spring的Maven项目,目前我的索引页已启动并在Tomcat上运行。 In my index page, I have: 在索引页面中,我有:

 <a href="about">about</a>

Then in the WEB-INF folder I have a subfolder called jsp which includes about.jsp. 然后在WEB-INF文件夹中,有一个名为jsp的子文件夹,其中包含about.jsp。 I'm getting stuck on how to have that href open the about.jsp as a webpage. 我对如何让href打开网页about.jsp感到困惑。 I tried creating a controller class, but I'm not sure if I'm doing it correctly. 我尝试创建控制器类,但是不确定是否正确执行了。 All I have in my controller is, 我在控制器中所拥有的就是

@Controller
public class AboutController {


 @RequestMapping("/about")
    public ModelAndView helloWorld() {

        String message = "Hello World";
        return new ModelAndView("about", "message", message); 
        }


  }

The servlet mapping in web.xml looks like. web.xml中的servlet映射如下所示。

<servlet-mapping>
<servlet-name>springMVC</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>

And then my springMVC-servlet.xml 然后是我的springMVC-servlet.xml

<context:component-scan base-package="com.springMVC.controller" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>


<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
<mvc:annotation-driven />

I've tried a few various tutorials but they all haven't worked. 我尝试了一些教程,但都没有用。

I should also note if I start up tomcat and go to localhost:8080/Test/about.jsp it works, I just can't get the linking working. 我还应该注意,如果我启动tomcat并转到localhost:8080 / Test / about.jsp,它可以工作,但是我无法使链接正常工作。

if I start up tomcat and go to localhost:8080/Test/about.jsp it works 如果我启动tomcat并转到localhost:8080 / Test / about.jsp,它会起作用

It seems you have placed about.jsp in web rather than WEB-INF/jsp . 似乎您已将about.jsp放置在web而不是WEB-INF/jsp Try moving about.jsp to given folder WEB-INF/jsp . 尝试将about.jsp移至给定的文件夹WEB-INF/jsp

您应该像这样<a href="/about">about</a>更改

You should change 你应该改变

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

to like this 喜欢这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM