我想在struts 2上创建一个简单的登录表单,但由于某种原因我在查看输入字段时遇到问题,并且在我提交用户的名称后没有出现......
这是代码
Struts重定向到我的struts.xml
我的struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<!-- devMode equals mode debug information and reload everything for every request -->
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/User" extends="struts-default">
<action name="Login">
<result>Login.jsp</result>
</action>
<action name="DashboardAction" class="action.DashboardAction">
<result name="success">Dashboard.jsp</result>
</action>
</package>
</struts>
Bean类
DashboardAction.java
package action;
public class DashboardAction {
private String username;
public String execute(){
return "success";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
JSPs
Login.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<h1>Struts 2 Login Test</h1>
<form action="DashboardAction" id="form1" method="post" autocomplete="off">
<div class="input placeholder">
<s:textfield name="username" label="Utilizador"/>
</div>
<div class="input placeholder">
<s:password name="password" label="Password"/>
</div>
<div class="submit">
<s:submit value="Entrar" method="execute"/>
</div>
</form>
</body>
</html>
Dashboard.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome User</title>
</head>
<body>
<h1>Hello
<s:property value="username"/>
</h1>
</body>
</html>
在我按提交后,为什么没有这项工作?应该去Dashboard.jsp
?
答案 0 :(得分:1)
你走了:
<result name="success">/Dashboard.jsp</result>
您需要在结果中指定JSP的完整路径。请注意/
。您需要对其他结果页面执行相同操作,包括Login.jsp
答案 1 :(得分:0)
您应该将表单映射到接受提交数据的操作。使用Struts标签
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:form action="Welcome" ...
<s:textfield name="username" ...
<s:password name="unknown" ...
无法映射密码字段,因为在您的操作中没有属性。如果你创建它可能是
private String unknown;
public String getUnknown() {
return unknown;
}
public void setUnknown(String unknown) {
this.unknown = unknown;
}
答案 2 :(得分:0)
在您的操作类中定义属性密码,如下所示。
<强> DashboardAction.java 强>
package action;
public class DashboardAction {
private String username;
private String password; // you don't have this line in your action class.
public String execute(){
return "success";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}