求教个SpringMVC如何处理前台传递过来的参数代码题,大佬看看对不对

qq_46016763 2022-06-10 10:30:44

使用SpringMVC框架,编码实现用户登录功能。 

要求如下:

(1)编写一个jsp页面(login.jsp),用户可以在页面输入用户名、密码。

(2)在jsp页面中点击“登录”按钮时,调用LoginController中的checkLogin()

方法获取用户输入的登录信息,如果用户名为“Tom”并且密码为“123456”,

则转向到success.jsp页面,否则,转向到failure.jsp页面。

(3)通过springMVC标签库渲染登录页面的表单元素。

(4)写出login.jsp页面和LoginController的核心代码即可。

<!--login.jsp-->
<%@ page language="java" contentType="textml; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="textml; charset=UTF-8">
<title>登录界面</title>
<style type="texts">
    *{margin: 0;padding: 0;}
    form{margin: 0 auto;padding:15px; width: 300px;height:300px;text-align: center;}
    #submit{padding: 10px}
    #submit input{width: 50px;height: 24px;}
<yle>
<script>
// 判断是登录账号和密码是否为空
function check(){
    var userName = $("#username").val();
    var password = $("#password").val();
    if(usercode=="" || password==""){
    	$("#message").text("账号或密码不能为空!");
        return false;
    }  
    return true;
}
</script>
</head>
<body>
    <div class="wrapper">
        <form action="<%=request.getContextPath()%>/loginDemo" method="post">
            <label>用户名:</label>
                <input type="text" name="userName" value="${param.userName}"/><br><br>
            <label>密码:</label>
                <input type="password" name="password"/><br>
                
            <font color="red">
                <%
                    if(request.getAttribute("message")!= null){
                        out.print(request.getAttribute("message"));
                    }
                %>
            </font>
            
            <div id="submit">
                <input type="submit" value="登录"/>
            </div>
        </form>
    
    </div>
</body>
<ml>
//LoginController
package loginDemo;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@PostMapping(value = "login")
public class LoginController extends HttpServlet {
    
    private static final long serialVersionUID = 1L;
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        System.err.println(userName+";"+password);
        String myUser = "Tom";
        String myPwd = "123456";
        if(userName.equals(myUser)&&password.equals(myPwd)) {
              return "success";
        }
        else return "failure";
    }
}

 

...全文
73 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
luzhe3014151 2022-06-22
  • 打赏
  • 举报
回复

这么看没啥问题呀,你是报什么异常了吗?

51,408

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧