配置struts-config.xml文件內容下面具體講解
開發第一個Struts程序
web.xml如上
struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<!-- 指定每次請求到達,重新加載資源文件 -->
<constant name="struts.i18n.reload" value="true"/>
<!-- 指定每次配置文件更改後,自動重新加載 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 把主題配置為simple -->
<constant name="struts.ui.theme" value="simple"/>
<package name="Struts" extends="struts-default" namespace="/">
<action name="login" class="com.linuxidc.action.LoginAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
LoginAction.java
package com.linuxidc.action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* @author 偶my耶
* action
*/
public class LoginAction extends ActionSupport {
/**
*
*/
HttpServletRequest request = ServletActionContext.getRequest();
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
String name=request.getParameter("user_name");//獲得user_name
request.setAttribute("name",name);//設值
return SUCCESS;
}
}
jsp界面
index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<form action="login.action">
姓名:<input type="text" name="user_name">
<input type="submit" value="跳轉">
</form>
</body>
</html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<% String name=(String)request.getAttribute("name");
%>
<body>
歡迎<%=name %>
跳轉成功!!
</body>
</html>
Struts工作原理
Struts是MVC設計模式的一種具體的實現;
Struts中的核心類就是ActionForm、Action,都需要在struts-config.xml文件中進行配置。
struts2文件上傳(保存為BLOB格式) http://www.linuxidc.com/Linux/2014-06/102905.htm
Struts2的入門實例 http://www.linuxidc.com/Linux/2013-05/84618.htm
Struts2實現ModelDriven接口 http://www.linuxidc.com/Linux/2014-04/99466.htm
遇到的Struts2文件下載亂碼問題 http://www.linuxidc.com/Linux/2014-03/98990.htm
Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm
Struts2 注解模式的幾個知識點 http://www.linuxidc.com/Linux/2013-06/85830.htm
Struts 的詳細介紹:請點這裡
Struts 的下載地址:請點這裡