SpringMVC:前台jsp页面和后台传值方法总结以及详细代码实现

  • 时间:
  • 来源:互联网
  • 文章标签:

SpringMVC:前台jsp页面和后台传值方法总结以及详细代码实现

首先先建了一个实体类

public class User implements Serializable {
    private String userName;
    private String pwd;

    public User(){

    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "User{" +
                "userName='" + userName + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }
}

然后建 index.jsp的登录页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<form action="login10.do"   method="post">
    账号:<input type="text" name="userName"/>
    密码:<input type="password" name ="pwd">
    <input type="submit" value="登录">


</form>
</body>
</html>

接着是前台页面hello1.jsp

<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--<%User user= (User) request.getAttribute("user");%>--%>

    <p>账号:</p>
    <p>密码:</p>

</body>
</html>

下面是几种传参方式

//向页面传值:方式一,通过Request
    @RequestMapping("/login5.do")
    public String loginR(User user,HttpServletRequest request){
        request.setAttribute("user1",user);
        return "hello1";
    }
    //页面传值:方法二,通过保存到Map集合返回ModeAndView对象
    @RequestMapping("/login6.do")
    public ModelAndView longinR(User user){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("user2",user);
        return new ModelAndView("hello1",map);
    }
    //向页面传值,方法三 通过ModelMap 将数据绑定页面
    @RequestMapping("/login7.do")
    public String loginModelMap(User user, ModelMap modelMap){
        modelMap.addAttribute("user3",user);
        return "hello1";
    }
    //向页面传值,方法四 通过session 向页面传值
    @RequestMapping("/login8.do")
    public String loginSession(User user, HttpSession session){
        session.setAttribute("user4",user);
        return "hello1";
    }

接下来是不同传参方式,hello1.jsp页面接收数据的不同代码实现
可以jsp实现

<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%User user= (User) request.getAttribute("user");%>

    <p>账号:<%=user.getUserName()%></p>
    <p>密码:<%=user.getPwd()%></p>

</body>
</html>

也可以EL实现

<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>


    <p>账号:${requestScope.user1.userName}</p>
    <p>密码:${requestScope.user1.pwd}</p>

</body>
</html>

用EL表达式方便许多

<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>


    <p>账号:${user2.userName}</p>
    <p>密码:${user2.pwd}</p>

</body>

<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>


    <p>账号:${user3.userName}</p>
    <p>密码:${user3.pwd}</p>

</body>

session和前面的有一点不同

<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>


    <p>账号:${sessionScope.user4.userName}</p>
    <p>密码:${sessionScope.user4.pwd}</p>

</body>
</html>

如果EL表达式显示不出来,
注意要把 isELIgnored=“false” 写上在 hello1.jsp中

<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>


    <p>账号:${sessionScope.user4.userName}</p>
    <p>密码:${sessionScope.user4.pwd}</p>

</body>
</html>

本文链接http://www.taodudu.cc/news/show-1944860.html