【问题标题】:Accessing JSP form data with in the Struts 2 Action class在 Struts 2 Action 类中访问 JSP 表单数据
【发布时间】:2016-03-08 18:31:00
【问题描述】:

我不明白如何在 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 类,其中包括带有 getter 和 setter 的 usernamepassword

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>

【问题讨论】:

  • 尽可能使用 struts 标签作为表单及其输入字段。将表单控件绑定到作为您的模型的操作 bean。请参阅开发者控制台了解错误。

标签: java jsp struts2


【解决方案1】:

使用 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"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 2013-12-17
    • 1970-01-01
    • 2015-05-22
    • 2013-06-11
    • 2016-08-29
    相关资源
    最近更新 更多