日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Struts2的自动装配

發布時間:2025/3/15 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Struts2的自动装配 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一種? ?零散參數的自動裝配

action方法

/**
*
* 零散參數自動裝配
*/
public class LoginAction implements Action {
private String username;
private String password;
public String execute() throws Exception {
if (username.equals("admin") && password.equals("admin")){
return SUCCESS;
}else {
return LOGIN;
}
}

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;
}
}

struts.xml的action配置文件 <!--零散參數的自動裝配-->
<action name="login" class="cn.sjl.day01.controller.LoginAction">
<result name="success">day01/success.jsp</result>
<result name="login">day01/login.jsp</result>
</action>

jsp頁面 <%@taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<s:form name="form1" namespace="/" method="post" action="login">
請輸入用戶名: <s:textfield name="username"></s:textfield> <br/>
<s:password name="password"></s:password><br/>
<s:submit value="登陸"></s:submit>
</s:form>
</body>
</html>


第二種 JavaBean(對象)類型的自動裝配

Action方法 public class UserInfo {
private String username;
private String password;

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;
}
}

/**
*
* JavaBean類型的自動裝配
*/
public class LoginBeanAction implements Action{
private UserInfo info;
public String execute() throws Exception {
if (info.getUsername().equals("admin")&& info.getPassword().equals("admin")){
return SUCCESS;
}else {
return LOGIN;
}
}

public UserInfo getInfo() {
return info;
}
public void setInfo(UserInfo info) {
this.info = info;
}
}

struts.xml的action配置 <!--JavaBean類型的自動裝配-->
<action name="loginbean" class="cn.sjl.day01.controller.LoginBeanAction">
<result name="success">day01/success.jsp</result>
<result name="login">day01/loginbean.jsp</result>
</action>

jsp頁面 <%@taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<s:form name="form1" namespace="/" method="post" action="loginbean">
請輸入用戶名: <s:textfield name="info.username"></s:textfield> <br/>
<s:password name="info.password"></s:password><br/>
<s:submit value="登陸"></s:submit>
</s:form>
</body>
</html>

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>成功 </title>
</head>
<body>
登錄成功
</body>
</html>


以上就是struts2自動裝配,總體來說struts2自動裝配還是挺簡單的。

轉載于:https://www.cnblogs.com/sujulin/p/8473026.html

總結

以上是生活随笔為你收集整理的Struts2的自动装配的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。