gpt4 book ai didi

java - 在 Spring 4.0.6 中无法将值从 Controller 发送到文件 jsp

转载 作者:行者123 更新时间:2023-12-01 09:34:08 25 4
gpt4 key购买 nike

我想从 Controller 发送值来查看jsp,但它只显示jsp的内容原始文本,类似:“Greeting:${greeting}”

任何人都可以帮我解决这个问题,将jsp中的“greeting”替换为“Hello World from Spring 4 MVC”。

谢谢!

这是项目的结构

This is structure of project

HelloWorldController.java

@Controller
@RequestMapping("/")
public class HelloWorldController {

@RequestMapping(method = RequestMethod.GET)
public String sayHello(ModelMap model) {
model.addAttribute("greeting", "Hello World from Spring 4 MVC");
return "welcome";
}

@RequestMapping(value = "/helloagain", method = RequestMethod.GET)
public String sayHelloAgain(ModelMap model) {
model.addAttribute("greeting", "Hello World Again, from Spring 4 MVC");
return "welcome";
}
}

spring-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

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

<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>

</beans>

web.xml

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Spring4MVCHelloWorldDemo Web Application</display-name>

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

欢迎.jsp

<body>
Greeting : ${greeting}
</body>

最佳答案

我通过将 <%@ page isELIgnored="false"%> 添加到我的顶级 jsp 来解决我的问题。

此解决方案由:https://stackoverflow.com/a/1530422/4768616

关于java - 在 Spring 4.0.6 中无法将值从 Controller 发送到文件 jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39150257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com