标签:package style 默认 3.1 space isp list lis over
一.Struts2基础
1.Struts2是有Apache在Struts1和Webwork的基础上研发出的新一代MVC框架。
2.Struts2开发环境的搭建:
打开https://struts.apache.org/download.html页面下载Struts2的版本,我使用是:struts-2.3.14-all.zip
二.HelloWorld示例
导入到项目中,然后加载到项目中(自行百度不细说)
2.新建一个FirstAction.java
package com.scce.struts2.action; import java.text.SimpleDateFormat; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class FirstAction extends ActionSupport { private String date; private String message; @Override public String execute() throws Exception { SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH时ss分mm秒"); date=sdf.format(new Date()); message="Hello World"; return SUCCESS; } public String getDate(){ return date; } public String getMessage(){ return message; } }
3.新建一个struts.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="action" namespace="/" extends="struts-default"> <action name="FirstAction" class="com.scce.struts2.action.FirstAction"> <result>/firstStruts2.jsp</result> </action> </package> </struts>
4.修改web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
5.新建一个firstStruts2.jsp页面
<body> ${message } <s:property value="date"/><br> ${date} </body>
6.浏览器访问地址:http://localhost:8080/struts2_01/FirstAction.action
效果如下图:
三.属性驱动模型
(注解:Struts2与ServletAPI实现了解耦,所以无法直接使用HttpServletRequest对象获取表单提交的参数,但Strtus2提供了属性驱动模型机制来解决问题)
<form action="UserManagerAction.action" method="post"> <p>用户名:<input type="text" name="username"/>${messsage}</p> <p>密 码:<input type="text" name="password"/></p> <input type="submit" value="提交"/> </form>
package com.scce.struts2.action; import com.opensymphony.xwork2.ActionSupport; public class UserManagerAction extends ActionSupport { public String getMesssage() { return messsage; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } private String username;//用户名 private String password;//密码 private String messsage;//消息 @Override public String execute() { System.out.println("用户名"+username);//打印用户名 System.out.println("密码"+password);//密码 //默认账号admin,密码1234验证登陆成功 if("admin".equals(username)&&"1234".equals(password)) { messsage="登陆成功!?"; return "success"; } else { messsage="对不起账号或密码错误"; return "error"; } } }
3.在struts.xml文件中配置UserManagerAction类的相关信息
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="action" namespace="/" extends="struts-default"> <action name="FirstAction" class="com.scce.struts2.action.FirstAction"> <result>/firstStruts2.jsp</result> </action> <action name="UserManagerAction" class="com.scce.struts2.action.UserManagerAction"> <result name="success">/login.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
4.在浏览器访问地址:
http://localhost:8080/struts2_01/UserManagerAction.action
标签:package style 默认 3.1 space isp list lis over
原文地址:https://www.cnblogs.com/2828sea/p/9638156.html