喜 尝试构建我自己的spring应用程序,我创建了一个页面来输入名称和年龄。单击Submit按钮应该带我到另一个显示用户输入的页面。我决定不使用任何数据库。作为第一步,我决定用一个方法创建一个控制器来显示表单视图。在我做对了之后,我会实现其他方法..
package personinfo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import personinfo.form.PersonInfo;
@Controller
public class PersonInfoController {
@RequestMapping(value ="/addPerson" )
public ModelAndView showAddPersonForm(){
return new ModelAndView("addperson", "personInfoEntries", new PersonInfo());
}
@RequestMapping(value ="/addPerson" ,method =RequestMethod.POST)
public String addPersonInfo(@ModelAttribute("person")PersonInfo perInfo){
String name = perInfo.getName();
int age = perInfo.getAge();
System.out.println("Name:" + name +"Age:"+ age);
return "showperson";//how to pass name and age
}
...
}
表单后备对象是personinfo.form.PersonInfo类
package personinfo.form;
public class PersonInfo {
private String name;
private int age;
public PersonInfo(){
}
public PersonInfo(String name,int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
/WEB-INF/jsp/addperson.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Person</title>
</head>
<body>
<h2>Add Person</h2>
<form:form action="addPerson.html" commandName="personInfoEntries">
<table>
<tr>
<td>
<form:label path="name">Name</form:label>
</td>
<td>
<form:input path="name"/>
</td>
</tr>
<tr>
<td>
<form:label path="age">Age</form:label>
</td>
<td>
<form:input path="age"/>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add Person"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
perinfo / WebContent / index.jsp是
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>index</title>
</head>
<body>
<jsp:forward page="addPerson.html"></jsp:forward>
</body>
</html>
/perinfo/WebContent/WEB-INF/web.xml
<?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>personinfo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
最后是/perinfo/WebContent/WEB-INF/dispatcher-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:annotation-driven/>
<context:annotation-config />
<context:component-scan base-package="personinfo.controller" />
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp">
<property name="contentType" value="text/html; charset=utf-8" />
</bean>
</beans>
正如chris指出的那样,我将@Controller添加到控制器类和servlet.xml中。 现在我可以看到表单了。当我点击提交按钮时,会显示showperson.jsp。太好了。
当执行PersonInfoController中的addPersonInfo()方法时,我想将名称和年龄值传递给showperson.jsp。我该怎么做?我正在返回一个'showperson'视图名称字符串。如何将参数添加到它?我无法弄清楚如何实现此视图名要映射到的相应方法。
有人可以告诉我吗?或者我是否需要修改addPersonInfo()方法的返回类型。
请帮忙。
感谢,
标记
答案 0 :(得分:1)
我建议你阅读我写的这个教程 Spring Security 3 - MVC Integration Tutorial (Part 1) 这样您就可以将您缺少的项目与应用程序进行比较。
只需看一下你的代码:
public class PersonInfoController {
@RequestMapping(value ="/addPerson" )
public String showAddPersonForm(){
System.out.println("showing AddPersonForm");
return "addperson";
}
...
}
我注意到你错过了:
@Controller
@RequestMapping("/mycustommapping")
通常我会像这样编写你的代码:
@Controller
@RequestMapping("/main")
public class PersonInfoController {
@RequestMapping(value ="/addPerson" )
public String showAddPersonForm(){
System.out.println("showing AddPersonForm");
return "addperson";
}
...
}
所以现在URL应该是/ main / addPerson
同样在您的dispatcher-servlet.xml中,您没有
<context:annotation-config />
答案 1 :(得分:0)
要回答您的后续问题,
当addPersonInfo()方法中 PersonInfoController执行我 想通过名字和年龄 showperson.jsp的值。我该怎么办? 这个?我正在回归“演艺人员” 查看名称字符串。如何添加 它的论点?我无法工作 如何实现相应的 此视图名称的方法 映射。
您需要将名称和年龄值添加到模型中并将其传递给视图。你可以在addPerson()中完成。
@RequestMapping(value ="/addPerson" ,method =RequestMethod.POST)
public String addPersonInfo(@ModelAttribute("person") PersonInfo perInfo, Model model) {
String name = perInfo.getName();
int age = perInfo.getAge();
// Add name reference to Model
model.addAttribute("name", name);
// Add age reference to Model
model.addAttribute("age", age);
return "showperson";
}
然后在你的JSP页面中,你至少应该有这样的东西:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
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>Insert title here</title>
</head>
<body>
<h1>Person</h1><table>
<p>name: ${id} </p>
<p>age: ${id} </p>
</body>
</html>
在我提供的链接上查看我的其他教程。有很多这样的例子