Struts2初体验
2018-11-20 03:22:32来源:博客园 阅读 ()
1.Struts 2及其优势
Struts 2是一个MVC框架,以WebWork框架的设计思想为核心,吸收了Struts 1的部分优点
Struts 2拥有更加广阔的前景,自身功能强大,还对其他框架下开发的程序提供很好的兼容性
步骤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> <!-- 拦截所有的action --> <url-pattern>/*</url-pattern> </filter-mapping>
步骤2:在src下创建名称为struts.xml的配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 配置文件中只要添加以下配置,那么以后修改配置文件不用重启tomcat --> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <!-- 第一个action的例子 --> <action name="helloWorld" class="cn.happy.action.HelloWorldAction"> <result name="success"> index.jsp </result> </action> <!-- 登陆的action --> </package> <!-- Add packages here --> </struts>
步骤3:编写HelloWorldAction
package cn.happy.action; import com.opensymphony.xwork2.Action; public class HelloWorldAction implements Action{ private String name ; private String message; public String execute() throws Exception { setMessage("Hello"+getName()); return "success"; } }
步骤4:创建index.jsp页面
<div>
<h1>
<!--显示Struts Action中message的属性内容-->
<s:property value="message"/>
</h1>
</div>
<div>
<form action="helloWorld.action" method="post">
请输入您的姓名:
<input name="name" type="text" />
<input type="submit" value="提交" />
</form>
</div>
步骤5:通过浏览器访问
点击提交后结果
2.struts配置文件说明
<!-- 配置文件中只要添加以下配置,那么以后修改配置文件不用重启tomcat --> <constant name="struts.devMode" value="true" />
一个警告的解决
问题描述:No configuration found for the specified action: 'login.action' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
解析:
<s:form action="Login" method="post" namespace="/"> or <s:form action="/Login" method="post" >
3.登陆案例
步骤一:struts.xml文件
<!-- 登陆的action --> <action name="login" class="cn.happy.action.LoginAction"> <result name="success"> login/success.jsp </result> <result name="login"> login/login.jsp </result> </action>
步骤二:LoginAction类的创建
package cn.happy.action; import com.opensymphony.xwork2.Action; public class LoginAction implements Action{ private String username = ""; private String password = ""; public String execute() throws Exception { if (username.equals("1")&&password.equals("1")) { return SUCCESS; }else { return LOGIN; } } }
步骤三:创建登陆界面
<s:form name="form1" namespace="/" method="post" action="login">
请输入用户名: <s:textfield name="username"></s:textfield> <br/>
<s:textfield name="password"></s:textfield><br/>
<s:submit value="登陆"></s:submit>
</s:form>
步骤四:在浏览器中访问
4.登陆案例强化:关于自动装配问题
在开发中,通常会以JavaBean方式保存数据。所以可以有如下写法
Action类
Jsp页面
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 体验SpringBoot(2.3)应用制作Docker镜像(官方方案) 2020-06-07
- Struts2 为什么被淘汰?自己作死! 2020-05-14
- 【问题管理】-- Struts2配置struts.xml中Action访问报There 2020-04-17
- 【java框架】Struts2(2) -- Struts2配置 2020-04-13
- struts1与struts2的区别 2020-03-16
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash