简单的jsp页面
Hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="/helloInput1.do?username=mao1&password=123" >链接</a>
<a href="/helloInput3.do?ids=1&ids=2" >多条删除链接</a>
<br>
<form action="helloInput2.do" method="post">
ID:<input type="text" name="id" > <br>
Balance:<input type="text" name="balance" > <br>
Date : <input type="date" name="birthday"> <br>
UID:<input type="text" name="user.uid" > <br>
username:<input type="text" name="user.name" > <br>
<button type="submit">提交</button>
</form>
</body>
</html>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<h2>Hello World!</h2>
<!--简单数据类型 -->
${user}
<br>
<!--对象数据类型 -->
${requestScope.account.id} ${requestScope.account.balance}
<br>
<c:forEach items="${requestScope.accountList}" var="at">
${at.id} ${at.balance} <br>
</c:forEach>
<hr>
${m}
</body>
</html>
Controller层
InputController类
import cn.csy.account.entity.Account;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
@Controller
public class InputController {
@RequestMapping("/toHello")
public String hello(){
return "hello";
}
@RequestMapping("/helloInput1")
public String helloInput1(String username,String password){
System.out.println(username);
System.out.println(password);
//转向
String m = "forward:/index.do";
//重定向
String n = "redirect:/toFile.do";
return n;
}
@RequestMapping("/helloInput2")
public String helloInput2(@ModelAttribute("m") Account a, @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday){
System.out.println(a);
System.out.println(birthday);
return "index";
}
}
注意:这里的跳转路径简写的原因是因为在springmvc.xml配置文件中添加了视图解析器的原因
如下:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
对于转向和重定向来说,不能使用视图解析器
如上边代码helloInput1方法中的写法.
helloInput2中的参数@ModelAttribute(“m”) Account a可以理解为是将Account作为对象起别名为m,传递给jsp页面,然后再前端页面进行接收.而a则是从前端传递过来的参数封装到Account中的,然后命名为a,本类对象通过a调用Account对象.
如:
${m}