我是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中是否有任何错误代码。
答案 0 :(得分:1)
您可以将index.jsp文件放在WebContent级别而不是项目层次结构中的WEBINF文件夹中。
在您的请求中,uri编写 / index 并点击输入您的can redirect index.jsp页面作为弹簧上下文xml文件。