gpt4 book ai didi

java - 如何在MVC流程中使用模型

转载 作者:行者123 更新时间:2023-12-01 15:21:38 25 4
gpt4 key购买 nike

我有一个提供登录屏幕的 jsp 文件,提交表单后,控件必须转到 servlet。现在,我如何将表单中的值保存到模型(Bean 类)并让它们在 Controller 中使用。我没有使用任何框架,如struts、spring等。

我使用了以下代码,但出现错误

java.lang.NoClassDefFoundError: bean/LoginBean

我的代码是:

index.jsp:

     <form name="signin" method="post" action="LoginServlet">
<table>
<tr><td><font>USERNAME</font></td><td><input type="text" name="signin_uname" /></td></tr>
<tr><td><font>PASSWORD</font></td><td><input type="password" name="signin_pwd" /></td></tr>
<tr><td><input type="reset" value="RESET" /></td><td><input type="submit" value="LOGIN" /></td></tr>
</table>
</form>

LoginServlet.java

   @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
HttpSession session=request.getSession();
System.out.println(session);
try {
LoginBean login=new LoginBean();
login.setSignin_pwd("raviteja");
login.setSignin_uname("raviteja");
System.out.println(login.getSignin_uname());
System.out.println(login.getSignin_pwd());
} finally {

}
response.sendRedirect("");
}

LoginBean.java

public class LoginBean implements Serializable {
String signin_uname,signin_pwd;
public LoginBean() {

}

public String getSignin_pwd() {
return signin_pwd;
}

public void setSignin_pwd(String signin_pwd) {
this.signin_pwd = signin_pwd;
}

public String getSignin_uname() {
return signin_uname;
}

public void setSignin_uname(String signin_uname) {
this.signin_uname = signin_uname;
}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>servlets.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

最佳答案

1 将Spring库添加到项目中,并在web.xml中映射DispatherServlet来处理/LoginServlet

2 创建 bean(表单和 bean 字段的名称相同):

public class LoginData {
private String signin_uname;
private String signin_pwd;

// Getters and setters
}

3 创建 Controller :

@Controller
public class LoginController {

@RequestMapping(value = "/LoginServlet", method = RequestMethod.POST)
public String postLoginData(@ModelAttribute LoginData loginData) {

// All data from form will be at your model attribute bean. It will also will
// be putted at request
String userName = logigData.getSignin_uname();

return "loginResult.jsp";
}
}

关于java - 如何在MVC流程中使用模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10828088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com