<%--
Document : successView
Created on : May 2, 2010, 2:06:51 PM
Author : nbuser
--%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Enter Your Name</title>
</head>
<body>
<h1>Enter Your Name!</h1>
<form:form commandName="car" method="POST">
Brand:<br>
<form:input path="brand.name"/><br>
<form:input path="brand.country"/><br>
Price:<br>
<form:input path="price"/><br>
<input type="submit" value="OK">
</form:form>
</body>
</html>
carview.jsp
<%--
Document : carView
Created on : May 2, 2010, 2:06:25 PM
Author : nbuser
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Hello</title>
</head>
<body>
<h2>${name}</h2>
<h2>${country}</h2>
<h2>${price}</h2>
</body>
</html>
ApplicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean name="carService" class="Service.CarService" />
<!--bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" /-->
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
package service;
import controller.Car;
/**
*
* @author nbuser
*/
public class CarService {
public String sayName(Car car) {
return String.format("Name: %s /n", car.getBrand().getName());
}
public String sayCountry(Car car) {
return String.format("Country: %s /n", car.getBrand().getCountry());
}
public String sayPrice(Car car) {
return String.format("Price: %d", car.getPrice());
}
}
package controller;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author PTOSH
*/
public class Brand {
private String name;
private String country;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package controller;
import java.math.BigDecimal;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author PTOSH
*/
public class Car {
private Brand brand;
private BigDecimal price;
public Brand getBrand() {
return brand;
}
public void setBrand(Brand brand) {
this.brand = brand;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import service.CarService;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
/**
*
* @author PTOSH
*/
//@Controller
//@RequestMapping("car")
public class CarController extends SimpleFormController {
// @Valid
private CarService carService;
public CarController() {
//Initialize controller properties here or
//in the Web Application Context
setCommandClass(Car.class);
setCommandName("car");
setSuccessView("successView");
setFormView("carView");
}
// @RequestMapping(method = RequestMethod.GET)
// public String initForm(ModelMap model){
// //return form view
// return "car";
// }
public void setCarService(CarService carService) {
this.carService = carService;
}
//Use onSubmit instead of doSubmitAction
//when you need access to the Request, Response, or BindException objects
@Override
// @RequestMapping(method = RequestMethod.POST)
protected ModelAndView onSubmit(Object command) throws Exception {
Car car = (Car) command;
ModelAndView mv = new ModelAndView(getSuccessView());
mv.addObject("name", carService.sayName(car));
mv.addObject("country", carService.sayCountry(car));
mv.addObject("price", carService.sayPrice(car));
return mv;
}
}
错误
grave:加载应用程序时出现异常grave:上下文/MyCar取消部署失败grave:加载应用程序时出现异常:java.lang.IllegalStateException:ContainerBase.AddChild:Start:org.apache.catalina.LifecycleException:java.lang.IllegalArgumentException:java.lang.ClassNotFoundException:org.SpringFramework.web.context.ContextLoaderListener
1查看Spring参考文档,您正在使用旧的Spring样式(verbose)通过JSP文件访问对象。
更改自
<spring:bind path="Brand">
<spring:bind path="brand">
我正在开发一个控制器来控制系统上的寄存器。这是我的控制器代码: 这是我的观点: 这是我的tiles.xml: 我会继续努力解决这个问题,谢谢你的关注!
问题内容: 由于某些原因,即使在这里查看了多个示例之后,我似乎也无法解决此问题。 我正在尝试使用Spring 3 MVC处理表单,但是出现以下错误: 因此,我有点想解决这里的问题,但未能解决。这是我的方法 ReverseController类扩展了SimpleFormController: 我的app-servlet.xml定义控制器如下: 这是我的reverse.jsp中的形式: 这就是我的we
在我的基本spring mvc应用程序中,我遇到了一个例外。我使用的是。我的代码如下。 在我的调度器-servlet中 在我的JSP页面中 在我的控制器课上 一旦用户输入正确的用户名和密码,就会重定向到页面。这工作正常。但是用户输入了错误的登录凭据,这会引发此错误。 例外 组织。springframework。网状物util。NestedServletException:请求处理失败;嵌套的例外是
是的,我读到这是一个很常见的问题,但是阅读这些帖子并没有真正帮助我。 小故事是我想在showAllComments.jsp上提交一个表单 以下是控制器: 结果如下:Java . lang . illegalstateexception:bean名称“command”的BindingResult和plain target对象都不能作为请求属性 但您可能需要从一开始就了解整个故事:用户在索引上启动应用
我在使用spring表单标记库创建表单时遇到了一个异常 “BindingResult 和 Bean 名称'命令'的纯目标对象都不能用作请求属性” jsp 页面是索引.jsp bean类是: 控制器类是
我在web上查找了几乎所有与此问题相关的答案,但无法在代码中找出问题所在。 这是我的JSP页面。 当我删除 很好用。我可以和我的控制器沟通。所以问题与这条线有关。 我可能在XML文件中做错了什么。我是新来的这个春暖花开的员工,所以等待您的帮助。谢谢。 这是引发的异常