Struts入門初體驗
阿新 • • 發佈:2018-02-26
src user 引入 res 輸入 splay pre 創建 ace
一 ,入門小案例
1,首先引入三個jar
<dependency> <groupId>javaee</groupId> <artifactId>javaee-api</artifactId> <version>5</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.4.1</version> </dependency> <dependency> <groupId>org.apache.struts.xwork</groupId> <artifactId>xwork-core</artifactId> <version>2.3.4.1</version> </dependency>
2,配置web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <filter> <filter-name>struts1</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts1</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
3,創建實體類HelloAction並實現Action
public class HelloAction implements Action{ public String execute() throws Exception { return SUCCESS; } }
4,創建配置文件struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <action name="hello" class="cn.happy.controller.HelloAction"> <result name="success">index.jsp</result> </action> </package> </struts>
5,入門小案例就寫完啦,通過struts.xml中action的name名來訪問。
二 ,登陸小案例
1,創建實體類LoginAction
private String username; //用戶名 private String password; //登陸密碼 public String execute() throws Exception { //如果你的用戶名和密碼都是admin,return success.反之,走到login 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; } }
2,配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <action name="login" class="cn.happy.controller.LoginAction"> <result name="success">day01/success.jsp</result> <result name="login">day01/login.jsp</result> </action> </package> </struts>
3,開發視圖登陸頁面
<%@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"> 請輸入用戶名:</br> <s:textfield name="username"/> 請輸入密碼:<s:password name="password"/> <s:submit value="登陸"></s:submit> </s:form> </body> </html>
三,強化一下剛才的登陸案例,通過javaBean自動裝配
1,新建實體類UserInfo
2,修改剛才的LoginAction頁面
//植入實體類
private UserInfo info=new UserInfo();
public String execute() throws Exception { //如果你的用戶名和密碼都是admin,return success.反之,走到login 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;
}
}
3,修改一下登陸頁面
<%@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">
請輸入用戶名:</br> <s:textfield name="info.username"/>
請輸入密碼:<s:password name="info.password"/>
<s:submit value="登陸"></s:submit> </s:form> </body> </html>
4,登陸成功後success.jsp
5,這個時候登錄名已經自動裝配上了
四,Struts2訪問Servlet API
解耦方案一:: 對Servlet API進行封裝 ,借助ActionContext
Struts入門初體驗