gpt4 book ai didi

java - Spring:如何调用简单的 Controller ?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:36:31 25 4
gpt4 key购买 nike

我是 Spring 的新手,从简单的教程开始。我定义了简单的 jsp 和 Controller,然后将其映射到 xml 文档并运行它,但我所看到的只是一个没有我在 Controller 中传递的值的 wev 页面:

@Controller
public class HomeController {

@Autowired
private ExampleService exampleService;

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model model) {
model.addAttribute("serverTime", exampleService.getSystemTime());
model.addAttribute("appVersion", exampleService.getAppVersion());
return "home";
}
}

@Component
public class ExampleService {

@Value("#{appProperties.appVersion}")
private String appVersion;

/**
* Returns formatted system time.
*
* @return
*/
public String getSystemTime() {
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
return dateFormat.format(new Date());
}

public String getAppVersion() {
return appVersion;
}
}

<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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>Spring</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
<!-- <param-value>/WEB-INF/jdbc-config.xml</param-value> -->
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

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

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

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<annotation-driven />

<resources mapping="/resources/**" location="/resources/" />

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

<context:component-scan base-package="com.home.spring" />

enter image description here

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

<!-- Root Context: defines shared resources visible to all other web components -->

<util:properties id="appProperties" location="properties.properties"/>

我已经学习了一些教程并阅读了与我的工作相关的官方文档的网络部分,但我还不明白我的代码哪里有问题。

我坚持了下来。如果您有任何想法,请与我分享。谢谢。

最佳答案

它对我来说工作正常。相同的代码。你能不能也把 jsp 贴出来。

我有以下jsp:如果您的 jsp 看起来相似,请告诉我们。

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${serverTime}
</body>
</html>

编辑:从 web.xml 中删除所有内容并放入以下内容:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appServlet-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

关于java - Spring:如何调用简单的 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10835178/

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