我是Spring的新手,正在使用Spring Tool Suite来运行我的应用程序。我已经配置了我的控制器,如下所示,我试图在我的jsp上打印随机引号。但是当我在jsp中使用${randomQuote}
时,该值不会打印出来。
我的控制器
@Controller
public class MyDemoController {
String[] quotes={"To be or not to be - Shakespeare","Beauty lies in the eyes of the beholder",
"With great power comes great responsibility"};
@RequestMapping(value="/getQuote",method=RequestMethod.GET)
public String getRandomQuote(Model model)
{
int randomNumber=new Random().nextInt(quotes.length);
String randomQuote=quotes[randomNumber];
model.addAttribute("randomQuote",randomQuote);
return "quote";
}
}
我的servletConfig如下
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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.3.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.kimble.controllers"></context:component-scan>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"></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>Demo App</display-name>
<servlet>
<servlet-name>MyDemoApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/myServletConfig.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyDemoApp</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
我点击的网址:http://localhost:8080/Kimble/getQuote.html
任何帮助都将深受赞赏。
添加我的jsp详细信息
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My Demo App</title>
</head>
<body>
<h2>The Quote is:</h2>
<p>${randomQuote}</p>
</body>
</html>