Struts2 MyEclipse8.5
时间:2010-09-26 来源:没得不安逸
MyEclipse8.5 — Struts2
1、 新建web project
添加支持
2、 项目上点击右键——》myeclipse——》Add Struts Capabilities——》Struts2——》/*——》Finish。
至此IDE已经为你完成的struts2的支持注入
需要注意两个文件 已经自动生成
第一个是src下的struts.xml sturts的配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="struts2" namespace="/" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<result name="input">/login.jsp</result>
<result name="success">/result.jsp</result>
<result name="failer">/loginError.jsp</result>
</action>
</package>
</struts>
第二个是web.xml 配置struts2的过滤器
<?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">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<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>
3、 页面的编写
用struts2标签(非常强大)
在jsp页面添加一句
<%@ taglib prefix="s" uri="/struts-tags" %>
引入struts标签库
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'login.jsp' starting page</title>
</head>
<body>
<s:form action="login">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit label="submit"></s:submit>
</s:form>
</body>
</html>
4、 编写Action
相当struts1中Action+ActionForm
继承ActionSupport类(com.opensymphony.xwork2.ActionSupport);
Override execute()方法
package com.test.action;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.Point;
public class PointAction extends ActionSupport{
private Point point;
private int age;
private String username;
private Date date;
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
5、 在struts.xml中添加Action的映射配置(上面已有实例)