无法从控制器获取字符串值

Emreturka

我是Spring MVC的新手。我想从我的Controller类中获取一个字符串值到我的jsp页面。我已经对其进行了浏览,但是尝试了一下,却无法获取数据。这是我的代码,

@Controller
@RequestMapping(value = "/index")
public class MainPageController {

   @RequestMapping(method = RequestMethod.GET)
   public String buttonText(ModelMap modelMap) {
      if (Constants.loginStatus == false) {
          modelMap.addAttribute("buttonText", "Giriş");
      } else {
          modelMap.addAttribute("buttonText", "Çıkış");
      }
      return "index";
   }
 }

headerTemplate.jsp

 <div align="right">
    <button type="button"
        style="color: #fff; background-color: #5bc0de; border-color:       #46b8da; box-sizing: border-box; border: 1px solid transparent; margin-top: 10px; margin-right: 25px; border-radius: 6px;"
        data-toggle="modal" data-target="#myModal">${buttonText}      </button>

我的调度程序servlet;

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

   <context:component-scan base-package="com.mesutemre">     </context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>

   <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"></property>
    <property name="suffix" value=".jsp"></property>
   </bean>
</beans>

这是我的web.xml;

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>BlogProject</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
    <description></description>
    <display-name>dispatcher</display-name>
    <servlet-name>dispatcher</servlet-name>
    <servlet-  class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
 <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern></url-pattern>
</servlet-mapping>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

我该如何获取我的控制器到我的jsp页面中的字符串值。我的xml中是否有任何错误代码。

桑杰
  • 第一件事

您可以将index.jsp文件放在WebContent级别中,而不放在项目层次结构的WEBINF文件夹中。

  • 第二件事

在您的请求uri中,编写/ index并按Enter键,将您可以重定向的index.jsp页面作为spring上下文xml文件。

  • 测试目的在jsp文件中打印您的消息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章