Java框架篇---Struts入门
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Struts1</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
配置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文件中进行配置。
Struts 的详细介绍:请点这里
Struts 的下载地址:请点这里