81,094
社区成员
发帖
与我相关
我的任务
分享
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index</title>
</head>
<body>
<s:a action="hello">hello</s:a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>error</title>
</head>
<body>
<center>
${applicationScope.name }<br />
${sessionScope.name }<br />
${requestScope.name}<br />
</center>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<!-- Filter名称 -->
<filter-name>struts2</filter-name>
<!-- Filter入口 -->
<filter-class>org.apache.struts2.dispathcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<!-- Filter名称 -->
<filter-name>struts2</filter-name>
<!-- 截获的所有URL -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<!-- 开始页面 -->
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0 //EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<!-- 指定struts.xml文件的根元素 -->
<struts>
<!-- 配置包,包名为default,该包继承了struts 2框架的默认包struts-default -->
<package name="default" namespace="/" extends="struts-default">
<!-- 定义名为hello的Action,该Action的处理类为com.action.UserAction,并映射到hello.jsp的页面 -->
<action name="hello" class="com.action.UserAction">
<result name="hello">/hello.jsp</result>
</action>
</package>
</struts>
<struts.i18n.encoding value="UTF-8"/>
package com.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
private static final long serialVersionUID = 1L;
public String execute() throws Exception{
ActionContext context = ActionContext.getContext();
context.put("name", "request:tom");
context.getApplication().put("name", "application:tom");
context.getSession().put("name", "session:tom");
return "hello";
}
}
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>