struts2 的3种驱动 第一种方式

[size=x-large]第一种是:将所有的属性都写在action中:[/size]
[color=red]在action中 我只写了一个属性username :

action 中的内容:[/color]

package com.luhua.test;


public class UserAction {

//用户名
private String username ;



public void setUsername(String username) {
this.username = username;
}


public String execute(){

return "success" ;

}

public String user(){
System.out.println(username); //这里接收的是页面上传过来的值
return "success" ;

}

}


[color=red]在struts.xml中的配置是:[/color]

<?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.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="welcome" class="com.luhua.test.UserAction" method="execute">
<result name="success">index.jsp</result>
</action>
<action name="user" class="com.luhua.test.UserAction" method="user">
<result name="success">success.jsp</result>
</action>
</package>

</struts>

[color=red]jsp页面有两张,index.jsp 和 success.jsp 从index.jsp中输入用户名; 在action中打印出来[/color]

[color=red]index.jsp:[/color]

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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%>">

</head>

<body>
<form action="user" method="post">
username:<input type="text" name="username"/>
<input type="submit"/>
</form>
</body>
</html>

[color=red]success.jsp[/color]


<%@ 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%>">

</head>

<body>
user
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值