Struts的例子修改

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/xkzju2010/article/details/61416614

在上一篇文章中,对例子进行了分析,现在需求如下:

在正确登陆之后,需要将参数传递到jsp页面,欢迎界面。

<?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="example" namespace="/example" extends="default">

        <action name="HelloWorld" class="example.HelloWorld">
            <result>/example/HelloWorld.jsp</result>
        </action>

        <action name="Login_*" method="{1}" class="example.Login">
            <result name="input">/example/Login.jsp</result>
            <result name = "success">/example/Menu.jsp</result>
            <!-- <result type="redirectAction">Menu</result>-->
        </action>

        <action name="*" class="example.ExampleSupport">
            <result>/example/{1}.jsp</result>
        </action>

        <!-- Add actions here -->
    </package>
</struts>

观察到,上面是更改之后的。

在Login.jsp之后,result如果是input则跳转到/example/Login.jsp

如果是success,则转到/example/Menu.jsp中

        <action name="Login_*" method="{1}" class="example.Login">
            <result name="input">/example/Login.jsp</result>
            <result name = "success">/example/Menu.jsp</result>
            <!-- <result type="redirectAction">Menu</result>-->
        </action>

在原来的文件中

<result type="redirectAction">Menu</result>

表示,所有的都redirect到MenuAction中。


上说的result的name属性,来自Action中的execute方法

	public String execute() throws Exception {

		if (isInvalid(getUsername()))
			return INPUT;

		if (isInvalid(getPassword()))
			return INPUT;

		return SUCCESS;
	}

这里登陆成功之后,转到/example/Menu.jsp

在Menu.jsp中进行获取Action中的属性。

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!-- s:include value="Missing.jsp" /-->
<html>
<head>
<title>Menu</title>
</head>

<body>
	<p>
		<h3>
			Welcome, <s:property value="username"/>
		</h3>
	</p>
</body>
</html>
如上述所示,使用了
Welcome, <s:property value="username"/>
主要是struts中的含义。

直接获取传递过来的username成员,这个成员在Action中已经定义相应的getter和setter方法。

然后,可以直接通过EL表达式来直接引用成员变量啦。

对于此用法,见接下来的一个文章。





猜你喜欢

转载自blog.csdn.net/xkzju2010/article/details/61416614