beanhtml
用於建立一個JavaBean實例java
constantweb
用於Struts2默認行爲標籤apache
<!-- 配置web默認編碼集,至關於HttpServletRequest.setChartacterEncoding用法,配置了這個至關於配置了一個字符編碼過濾器 --> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <!-- 默認Struts2攔截的請求後綴是.action,也就是說咱們配置了該元素,會攔截.do的請求,而不是.action,要包含,則在value屬性中添加action,do便可 --> <constant name="struts.action.extension" value="do"></constant> <!-- 設置瀏覽器是否緩存靜態內容,默認值爲true,在開發階段建議關閉,防止修改後測試不到 --> <constant name="struts.serve.static.browserCache" value="false"></constant> <!-- 當struts配置文件修改後,系統是否自動從新加載該文件,默認爲false。開發階段建議設置爲true,配置好後,更改成false --> <constant name="struts.configuration.xml.reload" value="true"></constant> <!-- 開發模式下使用,這樣能夠打印除更加詳細的錯誤信息 --> <constant name="struts.devMode" value="true"></constant> <!-- 默認視圖主體 --> <constant name="struts.ui.theme" value="simple"></constant>
include瀏覽器
用於引入其它的xml配置文件緩存
packageapp
是包標籤,用於區分不一樣的請求文件的標籤,比方說網站前臺請求/網站後臺請求 -->jsp
<!-- name:包名,用於被別的包調用或繼承 extends:繼承哪一個包,會繼承該包下配置信息和攔截器等等 namespace:選填,url鏈接必須加入namespace屬性值。以下:/new/action.xxx --> <package name="test" namespace="/new" extends="struts-default"> <!-- action至關於之前的servlet的概念,對應一個請求,name爲請求的url地址;class爲處理請求的action類 --> <action name="login" class="com.studyStruts.action.LoginAction"> <!-- 至關於請求處理後要跳轉的位置,name爲return的值 --> <result name="success">/success.jsp</result> <result name="fail">/fail.jsp</result> </action> </package>
簡單登陸示例post
index.jsp測試
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <%=path %> <br /> <%=basePath %> <br /> <form action="<%=path%>/new/login.do" method="post"> 用戶名:<input type="text" name="username"> 密碼:<input type="password" name="password"> <input type="submit" value="提交"> </form> </body> </html>
LoginAction.java
private String username;
private String password;
public String execute(){
if(username.equals("admin") && password.equals("123")){
return "success";
}else{
return "fail";
}
}
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
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <constant name="struts.action.extension" value="do"></constant> <constant name="struts.ui.theme" value="simple"></constant> <package name="test" namespace="/new" extends="struts-default"> <action name="login" class="com.studyStruts.action.LoginAction"> <result name="success">/success.jsp</result> <result name="fail">/fail.jsp</result> </action> </package> </struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 與項目名稱一致 --> <display-name>StudyStruts2</display-name> <!-- 設置url不寫文件名時,默認訪問的頁面 --> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> <!-- 添加struts過濾器 --> <filter> <filter-name>StrutsFilter</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>StrutsFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
http請求時程序處理流程
配置文件鏈接點