struts2练习及巩固
1.Struts2下载解压后
 
 apps:该文件夹下包含了基于Struts2的示例应用,这些示例应用对于学习者是非常有用的资料。 
 
 
 docs:该文件夹下包含了Struts2的相关文档,包括Struts2的快速入门、Struts2的文档,以及API文档等内容。 
 
 
 lib:该文件夹下包含了Struts2框架的核心类库,以及Struts2等的第三方插件类库。 
 
 
 src:该文件夹下包含了Struts2框架全部源代码。 
 
2.Struts2简单登录部分代码
 
 loginForm.jsp 
 
<%@ 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><s:text name="loginPage"/></title>
</head>
<body>
<s:form action="login" method="post">
	<s:textfield name="username" key="user"/>
	<s:textfield name="password" key="pass"/>
	<s:submit key="login"/>
</s:form>
</body>
</html> 
 上面用到了Struts2的标签库 
 
 
 struts.xml 
 
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<!-- START SNIPPET: xworkSample -->
<struts>
    <!-- Some or all of these can be flipped to true for debugging -->
    <!-- 指定全局国际化资源文件 -->
    <constant name="struts.i18n.reload" value="false" />
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="false" />
    <!-- 所有Action都定义在packpage下 -->
    <package name="default" extends="struts-default">
        
        <action name="login" class="com.mkydy.action.LoginFormAction">
        <!-- 定义两个逻辑视图和物理资源之间的映射 -->
            <result name="error">error.jsp</result>
            <result name="success">welcome.jsp</result>
        </action>
    </package>
</struts>
<!-- END SNIPPET: xworkSample -->
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<p>欢迎<%=request.getSession().getAttribute("user") %>光临!</p>
</body>
</html>LoginFormAction.java
package com.mkydy.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginFormAction extends ActionSupport{
	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;
	}
	
	//定义处理用户请求的execute方法
	@Override
	public String execute() throws Exception {
		if(getUsername().equals("admin")&&getPassword().equals("admin")) {
			ActionContext context = ActionContext.getContext();
			Map<String, Object> session = context.getSession();
			session.put("user", getUsername());
			return SUCCESS;
		}
		return ERROR;
	}
	
}
3.Struts2开发流程
- 在web.xml中定义核心Filter来拦截用户请求
- 如果需要以POST方式提交请求,则定义包含表单数据的jsp页面。如果仅仅只是以GET方式发送请求,则无需经过这一步。
- 定义用户处理得Action类。
- 配置Action及处理结果和物理视图资源之间的对于关系(struts.xml)。
- 编写视图资源。
本文来自博客园,作者:SnailsH,转载请注明原文链接:https://www.cnblogs.com/SnailsWalk/p/17976263
 
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号