1.使用 myEclipse开发struts2
2.配置web.xml
3.配置struts.xml(和struts1 不同struts-config.xml)
4.Action
5.如果 web.xml 中的版本是 2.5 使用 tomcat 5.0 发布的时候 jsp 页面的el表达式 默认是 忽略的。也就是原型输出不会计算里面的表达式。 此时 可以利用 tomcate 6. 发布项目 或者 在jsp 页面 加入<%@ page isELIgnored="false" %>
或者 在web.xml中设置
配置myEclipse 的 tomcat jdk
双击myEclipse--new web project
在 struts2.2.3之前只需要导入 5个包 就可以利用strtus2.之后需要导入9个包
导入jar:
struts2-core.jar //struts2的核心库
xwork.jar //webwork的核心库
ognl.jar //OGNl表达式库 Struts2 支持 EL
freemarker.jar //表现层框架 定义了Struts2的可视化组件主题。
commons-logging.jar //日志管理
commons-fileupload.jar //文件上传
commons-io.jar // 可以看成是java.io的扩展
commons-lang.jar //包含了一些数据类型工具类
javassist-GA.jar //Javassist是一个开源的分析、编辑和创建Java字节码的类库。
2.配置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"> <!-- 配置struts2的核心拦截器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- 配置filter拦截的URL --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
3.配置struts.xml(和struts1 不同struts-config.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> <constant name="struts.i18n.encoding" value="UTF-8"/> <package name="welcome" namespace="/" extends="struts-default"> <action name="wel" class="com.luob.action.WelcomeAction"> <result name="success" type="redirect"> <param name="location">/welcome.jsp</param> <param name="str">${str}</param> </result> </action> </package> </struts>
4.Action
package com.luob.action;
import com.opensymphony.xwork2.ActionSupport;
public class WelcomeAction extends ActionSupport {
private String userName;//jsp页面的表单的name值和这一样 就可以取到jsp页面的值了
private String userPwd; //jsp页面的表单的name值和这一样 就可以取到jsp页面的值了
private String str; //定义私有变量,传递的字符串
@Override
public String execute() throws Exception {
str="欢迎您:"+userName+"!您的密码为:"+userPwd;
return SUCCESS;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
5.如果 web.xml 中的版本是 2.5 使用 tomcat 5.0 发布的时候 jsp 页面的el表达式 默认是 忽略的。也就是原型输出不会计算里面的表达式。 此时 可以利用 tomcate 6. 发布项目 或者 在jsp 页面 加入<%@ page isELIgnored="false" %>
或者 在web.xml中设置
<jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <el-ignored>false</el-ignored> <!-- <el-ignored>true</el-ignored> --> </jsp-property-group> </jsp-config>