Struts2动态指定结果

本文介绍了一个使用Struts2框架与JSP技术相结合的简单示例项目。项目通过配置struts.xml文件定义了请求处理流程,并在UserAction类中实现了具体的业务逻辑。此外,还展示了如何根据不同条件跳转到不同的JSP页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Jsp反问页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
	动态结果
	一定不要忘了为动态结果的保存值设置set get方法
	<ol>
		<li><a href="user/user?type=1">返回success</a></li>
		<li><a href="user/user?type=2">返回error</a></li>
	</ol>
  </body>
</html>
 

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>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
    <package name="login" extends="struts-default" namespace="/user">
        <action name="user" class="com.lbx.action.UserAction">
        	<result>${r}</result>
        </action>
    </package>
    
</struts>
 

UserAction代码
package com.lbx.action;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UserAction extends ActionSupport{
	private int type;
	private String r;
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public String getR() {
		return r;
	}
	public void setR(String r) {
		this.r = r;
	}
	@Override
	public String execute() throws Exception {
		if(type==1){r="/success.jsp";}
		if(type==2){r="/error.jsp";}
		return SUCCESS;
	}
	
	
}
 



success.jsp和error.jsp没写

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值