Soy nuevo en Spring y comienzo con tutoriales simples. Defino JSPs y el controlador, entonces lo asigna documento XML y ejecutarlo, pero sólo lo que tengo sierra es un mi página wev sin valores que he pasado en la controladora:Spring: ¿cómo invocar un controlador simple?
@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" />
<?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"/>
He aprendido algunos tutoriales y he leído una parte web de la documentación oficial que es relevante para mi trabajo, pero todavía no entiendo dónde hay un problema en mi código.
Estoy atascado con eso. Si tienes alguna idea, por favor compartela conmigo. Gracias.
¿Ha tenido un vistazo a la aplicación petclinic (http://static.springsource.org/docs /petclinic.html)? Quizás eso te ayude. – Christian
vi también la página jsp, que todo parece perfecto, ¿no hay errores? – NimChimpsky
¿Puede mostrarnos el error en la consola si corresponde? COMO tu jsp se ve bien para mí. El error podría darnos alguna pista – dharam