我是struts的新手用户。这是我第一个使用struts的应用程序。我不了解如何在Action类的jsp页面中访问表单数据
的login.jsp:
<div class="well">
<form id="loginForm" method="POST" action="hr/login/" novalidate="novalidate">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon" id="UserEmail"><i class="fa fa-user" title="Enter Your username"></i></span>
<input type="text" class="form-control" id="username" name="username" value="" required title="Please enter you username" placeholder="Enter Username" />
</div>
<span class="help-block"></span>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon" id="UserPasswordMatch"><i class="fa fa-lock" title="Choose password"></i></span>
<input type="password" class="form-control" id="passwordmatch" name="passwordmatch" value="" required title="Enter your password" placeholder="Enter Password" />
</div>
</div>
<button type="submit" class="btn btn-success btn-block">Login</button>
</form>
</div>
BookingAction.java:
public class BookingAction {
private String name;
HotelReservationServceImpl service = new HotelReservationServceImpl();
public String execute() throws Exception {
return "success";
}
public String loginExecute()
{
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我还有一个User
类,其中包含私有属性,包括username
和password
,包括getter和setter。
service.java:
public class HotelReservationServceImpl implements IHotelReservationService {
HotelReservationDAOImpl dbcon = new HotelReservationDAOImpl();
@Override
public int login(String username, String passwrd) {
if(username.isEmpty() || passwrd.isEmpty())
{
System.out.print(" Enter username and password ");
}
else
{
int i = dbcon.login(username, passwrd);
}
//dbcon.dbConnector();
return 0;
}
}
struts.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<!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.devMode" value="true" />
<package name="helloworld" extends="struts-default">
<action name="hello"
class="com.reservation.action.BookingAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
<action name="login"
class="com.reservation.action.BookingAction"
method="loginExecute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
答案 0 :(得分:0)
使用Struts标记将表单绑定到操作,并将输入字段绑定到操作属性。
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:form id="loginForm" method="POST" action="login" novalidate="novalidate">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon" id="UserEmail"><i class="fa fa-user" title="Enter Your username"></i></span>
<s:textfield cssClass="form-control" id="username" name="user.username" value="" required title="Please enter you username" placeholder="Enter Username" />
</div>
<span class="help-block"></span>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon" id="UserPasswordMatch"><i class="fa fa-lock" title="Choose password"></i></span>
<s:password cssClass="form-control" id="passwordmatch" name="user.password" value="" required title="Enter your password" placeholder="Enter Password" />
</div>
</div>
<button type="submit" class="btn btn-success btn-block">Login</button>
</s:form>
要绑定表单,您应该将操作名称设置为表单标记。要绑定输入字段,您应该将属性名称设置为struts输入标记。
属性位于User
bean中,您应该聚合到操作类。
private User user;
public User getUser() { return user; }
public void setUser(User user) { this.user = user; }
如果您不想为页面生成额外的HTML,请使用
<constant name="struts.ui.theme" value="simple"/>