Struts2提供了访问Servlet API的方式: Servlet API就是HttpServletRequest,HttpSession,ServletContext,这三个接口分别代表JSP内置对象中的request,session,application。 Struts2提供了一个ActionContext类,Struts2可以通过该类访问Servlet API;
1.配置web.xml文件:
<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>/*</url-pattern>
</filter-mapping>
2.配置struts.xml文件
<constant name="conststruts.devMode" value="true"/> //开发者模式,代表可以打印更详细的信息
<package name="Demo' namespace="/" extends="struts-default">
<action name="*">
<result>/WEB-INF/content/{1}.jsp</result> //对于任意的请求,直接呈现WEB-INF/content目录下同名的jsp文件
</action>
</package>
3.在/WEB-INF/content/目录下创建一个loginForm.jsp文件
<%@ page contentType="text/html;charset=GBK"%>
<%@ taglib prefix="s" uri="/struts-tags">
<html>
<head>
<title><s:text name="loginPage"/></title>
</head>
<body>
<s:form action="login">
<s:textfield name="username' key="user"/>
<s:textfield name="password" key="pass"/>
<s:submit key="login"/>
</s:form>
</body>
</html>
4.在使用loginForm.jsp之前还需要写一个配置文件用于国际化
mess.properties:
loginPage=登录页面
user=用户名
pass=密码
login=登录
succTip=成功页面
failTip=错误页面
5.国际化之后再在struts.xml中增加全局国际化的资源文件:
<constant name="struts.custom.i18n.resources" value="mess"/>
value="mess"指定了国际化资源文件名baseName为mess的资源文件
6.导入struts应用相应的jar包
7.在浏览器输入:http://localhost:8080/ActionContext/loginForm可以看到登录页面;
8.处理页面提交的action;
在src目录下创建Demo.LoginAction类;
package Demo;
import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext;
public class LoginAction implements Action { private String username; private String password;
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;
}
[@Override](https://my.oschina.net/u/1162528)
public String execute() throws Exception {
ActionContext context = ActionContext.getContext(); //获取ActionContext
Integer counter = (Integer)context.getApplication().get("counter");
if(counter==null){
counter = 1;
}else{
counter = counter+1;
}
context.getApplication().put("counter", counter); //设置application
context.getSession().put("username", getUsername()); //设置session
if(getUsername().equals("wxc")&&getPassword().equals("wxc")){
context.put("tip", "服务器提示:您已经成功登陆!"); //设置request
return SUCCESS;
}
context.put("tip","服务器提示:登陆失败!");
return ERROR;
}
}
9.在WEB-INF/content目录下创建welcome.jsp和error.jsp文件
welcome.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%> <%@ 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> <title><s:text name="succTip"/></title> </head> <body>
本站访问次数:${applicationScope.counter}<br/>
${sessionScope.username }您已经成功登录<br/>
${requestScope.tip }
</body> </html>
error.jsp <%@ page language="java" contentType="text/html; charset=GBK"%> <%@ 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> <title><s:text name="failTip"/></title> </head> <body> 本站访问次数为:${applicationScope.counter }<br/> ${sessionScope.username }您不能登录!<br/> ${requestScope.tip } </body> </html>
10.最后在struts.xml文件中配置提交的action; <action name="login" class="Demo.LoginAction"> <result name="success">/WEB-INF/content/welcome.jsp</result> <result name="error">/WEB-INF/content/error.jsp</result> </action>
代码完毕!!!