我正在学习 Spring MVC 3,但我被困在一个简单的问题上。我有一个 View 计算器,它在一个表格中接受 2 个整数。这些被传递到我的表单 pojo 中。我的控制器添加然后打印到 Catlina.out 日志。我想传递我的整数;回到视图我怎样才能实现这一点。
这是初始视图
<table>
<tr>
<td><form:label path="a">First Number</form:label></td>
<td><form:input path="a" /></td>
</tr>
<tr>
<td><form:label path="b">Second Number</form:label></td>
<td><form:input path="b" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add Numbers"/>
</td>
</tr>
这是我的控制器:
@RequestMapping(value = "/addNumbers", method = RequestMethod.POST)
public String addNumbers(@ModelAttribute("calculator")
Calculator calculator, BindingResult result) {
System.out.println(calculator.getA() + calculator.getB());
return "redirect:calculator.html";
}
更新了最初的问题得到回答后,我添加了一个新视图来显示结果并更改了我的控制器:
<%@ include file="/WEB-INF/jsp/include.jsp" %>
<html>
<head>
<title>Syed Shah</title>
</head>
<body>
<h2>Results</h2>
${calculator.A}
<a href="calculator.html">Back to Calculator</a>
</body>
</html>
我的计算器实例:
package webcalculator.form;
//import webcalculator.domain.Numbers;
public class Calculator {
private int A;
private int B;
private int sum;
public int getA() {
return A;
}
public void setA(int a) {
A = a;
}
public int getB() {
return B;
}
public void setB(int b) {
B = b;
}
public int getAddSum(){
sum = A+B;
return sum;
}
}
我的新控制器:
public class CalculatorController {
@RequestMapping(value = "/addNumbers", method = RequestMethod.POST)
public String addNumbers(Calculator calculator, BindingResult result, Model model) {
//System.out.println(calculator.getA() + calculator.getB());
model.addAttribute("calculator", calculator);
return "result"; // notice no redirect
}
堆栈跟踪:
org.apache.jasper.JasperException:在第 9 行处理 JSP 页面 /WEB-INF/jsp/result.jsp 时发生异常
6:7:
结果
8: 9: ${calculator.getAddSum} 10:
11: 返回计算器 12:堆栈跟踪:org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430) org.apache.jasper.servlet.JspServlet.serviceJspFile( JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.springframework.web.servlet.view。 InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238) org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264) org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208) org. springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992) org.springframework.web.servlet.DispatcherServlet。doDispatch(DispatcherServlet.java:939) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) org.springframework.web。 servlet.FrameworkServlet.doPost(FrameworkServlet.java:838) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) javax.servlet。 http.HttpServlet.service(HttpServlet.java:717)HttpServlet.service(HttpServlet.java:637) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)HttpServlet.service(HttpServlet.java:637) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
根本原因
javax.el.PropertyNotFoundException:在类型 webcalculator.form.Calculator 上找不到属性“getAddSum” javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:214) javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java: 191) javax.el.BeanELResolver.property(BeanELResolver.java:300) javax.el.BeanELResolver.getValue(BeanELResolver.java:81) javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54) org.apache.el。 parser.AstValue.getValue(AstValue.java:123) org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:938) org.apache。 jsp.WEB_002dINF.jsp.result_jsp._jspService(result_jsp.java:69) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet。http.HttpServlet.service(HttpServlet.java:717) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org. apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java: 238) org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264) org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208) org.springframework.web.servlet.DispatcherServlet。 processDispatchResult(DispatcherServlet.java:992) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939) org.springframework.web。servlet.DispatcherServlet.doService(DispatcherServlet.java:856) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838) javax. servlet.http.HttpServlet.service(HttpServlet.java:637) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)812)javax.servlet.http.HttpServlet.service(HttpServlet.java:717)812)javax.servlet.http.HttpServlet.service(HttpServlet.java:717)