-
有时候需要在浏览器的地址栏中直接输入url地址而去访问控制层来达到一些功能上的目的,比如列表
-
控制层处理指定的<form method="xxx">
核心代码如下:
`
package controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class UserinfoController {
@RequestMapping(value = "login", method = RequestMethod.POST)
public String loginMethod(String username, String password, Model model) {
if (username.equals("a") && password.equals("aa")) {
model.addAttribute("usernameKey", username);
return "ok.jsp";
} else {
return "no.jsp";
}
}
@RequestMapping(value = "listString")
public String listStringMethod(Model model) {
List list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
model.addAttribute("list", list);
return "listString.jsp";
}
}
`
index.jsp
`
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
post:
<br />
<form action="login.spring" method="post">
username:<input type="text" name="username">
<br />
password:<input type="text" name="password">
<br />
<input type="submit" value="submit"><br />
</form>
<br /> get:
<br />
<form action="login.spring" method="get">
username:<input type="text" name="username">
<br />
password:<input type="text" name="password"><br />
<input type="submit" value="submit"><br />
</form>
</body>
</html>
`
listString.jsp
`
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<c:forEach var="eachString" items="${list}">
${eachString}<br />
</c:forEach>
</body>
</html>`
出现异常没有发现处理get请求的方法
- 邮箱:ithelei@sina.cn
- 技术讨论群:687856230
- GoodLuck
网友评论