Struts2能受理的扩展名的问题
默认可受理的扩展名在struts2-core-3.2.16.1.jar包下的org.apache.struts2包下的default.properties文件中进行配置了Struts2应用的一些常量,里面还有设置上传文件大小的常量 ,编码常量等的配置
在需要修改的情况下不在该文件中修改 ,只需要在stauts.xml文件中进行修改即可
<constant name=”struts.action.extension” value=”action,do,”></constant>
index.jsp
<body>
<a href ="TestActionContext?name=atguigu">Test ActionContext</a>
<br/>
<br/>
<a href ="TestAware.action?name=atguigu">Test Aware</a>
<br/>
<br/>
<a href="TestServletActionContext">TestServletActionContext</a>
<br/>
<br/>
<a href="TestServletAware.action">TestServletAware</a>
<br/><br/>
<!-- 下面为一个小例子程序 -->
<a href="login-ui.do">LoginUI</a>
<%
if(application.getAttribute("date") == null)
application.setAttribute("date",new Date());
request.setAttribute("req","reqvalue");
%>
</body>
package com.atguigu.struts2.action;
import java.util.Map;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.SessionAware;
public class UserAction implements SessionAware,ApplicationAware{
private String username;
public void setUsername(String username) {
this.username = username;
}
public String execute(){
System.out.println("aaa");
//把用户存入 Session 域中
//1.获取session,通过实现SessionAware接口
//2获取登陆信息:用过在Action中添加setter方法
//3将用户信息放入Session中
session.put("username", username);
//在线人数 + 1
//1.获取当前的在线人数,从application中获取
Integer count = (Integer) application.get("count");
if(count == null){
count = 1;
}else{
//2.使当前的在线人数 +1
count++;
}
application.put("count", count);
return "login-success";
}
/**
* 这个为例子程序退出登陆所调用的方法
*
*/
public String logout(){
//1.数量减1: 获取在线人数,若数量还>0,则 - 1。
Integer count = (Integer) application.get("account");
if(count != null && count > 0){
count--;
application.put("count", count);
}
//2.session 失效:强转为SessionMap,调用invalidate方法
((SessionMap)session).invalidate();
return "logout-success";
}
private Map<String ,Object> session ;
public void setSession(Map<String, Object> session) {
this.session = session;
}
private Map<String,Object> application;
public void setApplication(Map<String, Object> application) {
this.application = application;
}
}
Struts.xml
<struts>
<!-- 配置Struts 可以受理的请求的扩展名 且这个扩展名的值 会把默认配置文件中配置的值覆盖掉-->
<constant name="struts.action.extension" value="action,do,"></constant>
<package name="default" namespace="/" extends = "struts-default">
<action name="TestActionContext" class="com.atguigu.struts2.action.TestActionContextAction">
<result>/test-actionContext.jsp</result>
</action>
<action name="TestAware" class="com.atguigu.struts2.action.TestAwareAction">
<result>/test-aware.jsp</result>
</action>
<action name="TestServletActionContext" class="com.atguigu.struts2.action.TestServletActionContextAction">
<result>/success.jsp</result>
</action>
<action name="TestServletAware" class="com.atguigu.struts2.action.TestServletAwareAction">
<result>/success.jsp</result>
</action>
<action name="login-ui">
<result>/login.jsp</result>
</action>
<!-- 简单的用例 -->
<action name="user-login" class="com.atguigu.struts2.action.UserAction">
<result name="login-success">/login-success.jsp</result>
</action>
<action name="logout" class="com.atguigu.struts2.action.UserAction"
method="logout">
<result name="logout-success">/login.jsp</result>
</action>
</package>
</struts>
Login.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>
</head>
<body>
<form action="user-login.do" method="post">
username:<input type="text" name="username"/>
<input type="submit" value="Login"/>
</form>
</body>
</html>
Login-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 'login-success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
Welcome: ${sessionScope.username }
<br/><br/>
Count On Line : ${applicationScope.count }
<br/><br/>
<a href="logout.do">Logout</a>
</body>
</html>