struts2.x入门实例
来源:互联网 发布:淘宝龟苓膏有假的吗 编辑:程序博客网 时间:2024/05/22 03:14
Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。其全新的Struts 2的体系结构与Struts 1的体系结构差别巨大。Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与ServletAPI完全脱离开,所以Struts 2可以理解为WebWork的更新产品。虽然从Struts 1到Struts 2有着太大的变化,但是相对于WebWork,Struts 2的变化很小。
其他的不多说了,直接开始实例编写。
1 创建Web项目
这是还是要说明一下,web项目的运行这是之前已经配置过相关的Server Runtime Enviroment,具体可以参考上篇博客中所做
2 在WebRoot下的WEB-INF下创建项目主配置文件web.xml
3 在WebRoot/WEB-INF下创建classes文件夹,并创建struts2配置文件struts.xml
4 在WebRoot下创建放jsp页面的文件夹page
5 配置web.xml
<?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"> <!-- 指定struts2的配置文件 --> <context-param> <param-name>contextConfigLocation </param-name> <param-value>/WEB-INF/classes/struts.xml </param-value> </context-param> <!-- 配置过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- 指定过滤规则 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>/page/login.jsp</welcome-file> </welcome-file-list></web-app>
6 定义实现LoginAction
package com.peace.action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginAction 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; } public String execute() throws Exception { if (getUsername().equals("test") && getPassword().equals("123")) { ActionContext.getContext().getSession().put("user", getUsername()); return SUCCESS; }else{ return ERROR; } }}
7 在classes下创建并配置之后需要乃到的全局资源文件local.properties和编码字符集local_zh_CN.properties
local.properties
loginPage=loginPageerrorPage=errorPagesuccPage=succPagefailTip=sorry\uFF0Clogin failedsuccTip=welcome{0},login successuser=usernamepass=passwordlogin=login
local_zh_CN.properties
loginPage=\u767B\u9646\u754C\u9762errorPage=\u5931\u8D25\u754C\u9762succPage=\u6210\u529F\u754C\u9762failTip=\u5BF9\u4E0D\u8D77\uFF0C\u60A8\u4E0D\u80FD\u767B\u5F55\uFF01succTip=\u6B22\u8FCE\uFF0C{0},\u60A8\u5DF2\u7ECF\u767B\u5F55\uFF01user=\u7528\u6237\u540Dpass=\u5BC6 \u7801login=\u767B\u9646
8 配置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> <!-- 指定全局配置资源文件 --> <constant name="struts.custom.i18n.resources" value="local"/> <!-- 指定全局编码所使用的字符集 --> <constant name="struts.i18n.encoding" value="GBK"/> <!-- 所有的Action定义都应该放在package下 --> <package name="test" extends="struts-default"> <action name="loginverify" class="com.peace.action.LoginAction"> <result name="error">/page/error.jsp</result> <result name="success">/page/welcome.jsp</result> </action> </package></struts><!-- END SNIPPET: xworkSample -->
9 在page下创建相关的jsp页面
login.jsp
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%><%@taglib prefix="s" uri="/struts-tags"%><html><head><meta http-equiv="Content-Type" content="text/html; charset=GBK"><title><s:text name="loginPage"/></title></head><body><s:form action="loginverify"> <s:textfield name="username" key="user"/> <s:textfield name="password" key="pass"/> <s:submit key="login"/></s:form></body></html>
welcom.jsp
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%><%@taglib prefix="s" uri="/struts-tags"%><html><head> <title><s:text name="succPage"/></title><meta http-equiv="Content-Type" content="text/html; charset=GBK"></head><body> <s:text name="succTip"> <s:param>${sessionScope.user}</s:param> </s:text><br/></body></html>
error.jsp
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%><%@taglib prefix="s" uri="/struts-tags"%><html><head> <title><s:text name="errorPage"/></title> <meta http-equiv="Content-Type" content="text/html; charset=GBK"></head><body> <s:text name="failTip"/></body></html>
10 运行测试结果
总结
struts2请求处理过程
- struts2.x入门实例
- Struts2入门实例
- Struts2 入门实例代码
- Struts2 入门实例
- Struts2入门实例
- Struts2入门经典实例
- Struts2的入门实例
- Struts2简单入门实例
- Struts2入门经典实例
- Struts2开发入门实例
- Struts2入门实例
- struts2入门实例
- SSH-struts2入门实例
- struts2 helloworld入门实例
- struts2入门实例
- Struts2入门实例
- Struts2 入门实例
- struts2入门实例
- PHP中的进制
- 我的Python第四篇 字典与集合篇
- Tomcat学习之虚拟目录
- jQuery自定义元素右键点击事件
- class文件中的字段表集合--field字段在class文件中是怎样组织的
- struts2.x入门实例
- class文件中的方法表集合--method方法在class文件中是怎样组织的
- eclipse安装maven插件问题总结
- 关于深户办理户口本和身份证的总结
- 数据结构-不相交集ADT
- Linux 字符集设置
- leetcode 299. Bulls and Cows
- JVM机器指令集
- office2013打开时出现向程序发送命令时出现问题的另一种解决方法