Struct2 helloworld及基本action
struct2不再跟struct一样依赖于Servlet API和Struts API,提供了拦截器,类型转换器,且提供了jsp,freemaker等表现层技术
1.web.xml
struct2基本的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">
<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>
</web-app>
action默认的配置
<struts>
<package name="default" namespace="/" extends="struts-default">
<!- 指明action具体的类-!>
<action name="/" class="...">
<result name="success">/hello.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
1>action默认的class是ActionSupport。
2>如果没有为action指定method,默认执行action中的execute() 方法。
3>result的name属性默认值为success。
如下是一个简单的struct2实现action的登录(成功显示书籍资料)
1.booklist类
package csc.struts2.booklist;
public class Booklist {
String[] books=new String[]{
"java入门到死亡",
"PHP世界最好语言",
"垃圾net毁一生",
"ssh框架"
};
public String[] getall(){
return books;
}
}
2.action接口形式,result的默认name主要一下几种
package csc.struts2.interf;
public interface Action {
public static final String SUCCESS="success";
public static final String ERROR="error";
public static final String NONE="none";
public static final String INPUT="input";
public static final String LOGIN="login";
//默认的action方法
public String execute() throws Exception;
}
3.实现action
package csc.struts2.booklist;
import com.opensymphony.xwork2.ActionContext;
import csc.struts2.interf.Action;
public class userlist implements Action{
private String username;
private String password;
private String[] book;
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[] getBook() {
return book;
}
public void setBook(String[] book) {
this.book = book;
}
@Override
public String execute() throws Exception {
//获取用户名
String username=(String) ActionContext.getContext()
.getSession().get("user");
if ( username!=null&&
getUsername().equals(username)){
//存book[]
Booklist books=new Booklist();
setBook(books.getall());
return SUCCESS;
}else {
return ERROR;
}
}
}
已jsp做表现层
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
<form action="show.action">
<p>用户名<input type="text" name="username" value="">
<br>
<p>密码<input type="text" name="password"></p><br />
<input type="submit" value="登录" />
</form>
</body>
</html>
struct2.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="default" namespace="/" extends="struts-default">
<action name="show" class="csc.struts2.booklist.userlist">
<result name="success">/showbook.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
注:
ActionSupport类的作用
struts2不要求我们自己设计的action类继承任何的struts基类或struts接口,
但是我们为了方便实现我们自己的action,大多数情况下都会
继承com.opensymphony.xwork2.ActionSupport类,并重写此类里的
public String execute() throws Exception方法。
因为此类中实现了很多的实用借口,提供了很多默认方法,这些默认方法包括国际化信息的方法、默认的处理用户请求的方法等,这样可以大大的简化Acion的开发。