1.首先搭建Struts2的环境
新建Web工程后,将Struts2的架包(可到Struts的官方下载)考到lib目录下面。
1.首先搭建Struts2的环境
新建Web工程后,将Struts2的架包(可到Struts的官方下载)考到lib目录下面。
2.首先创建一个Action类
Java代码
package com.wl.control.tags;
import com.opensymphony.xwork2.ActionSupport;
public class ShowCity extends ActionSupport {
private int cityId;
public int getCityId() {
return cityId;
}
public void setCityId(int cityId) {
this.cityId = cityId;
}
@Override
public String execute() throws Exception {
this.cityId=2;
return "showcity";
}
}
3.创建JSP页面
Java代码
lt;%@ page language="java" import="java.util.*" pageEncoding="GB18030"%&
lt;%@ taglib uri="/struts-tags" prefix="s" %&
lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&
lt;html&
lt;body&
lt;s:if test="cityId==1"&北京
lt;s:elseif test="cityId==2"&上海
lt;s:else&杭州
lt;/body&
lt;/html&
4.配置文件Web.xml文件
Java代码
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
lt;filter-mapping&
lt;filter-name&struts2
lt;url-pattern&/*
lt;/filter-mapping&
lt;welcome-file-list&
lt;welcome-file&index.jsp
lt;/welcome-file-list&
lt;/web-app&
5.Struts.xml配置文件
Java代码
lt;?xml version="1.0" encoding="UTF-8"?&
lt;!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
lt;struts&
lt;package name="Struts2.1" extends="struts-default"&
lt;action name="showcity" class="com.wl.control.tags.ShowCity"&
lt;result name="showcity"&/controlTags/showcity.jsp
lt;/action&
lt;/package&
lt;/struts&
6.运行效果如下