index.jsp
<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h3>Welcome To Struts 2!</h3>
<p><a href="<s:url action='hello'/>">Hello User</a></p>
<s:form action="hello">
<s:textfield name="userName" label="用户名" />
<s:submit value="提交" />
</s:form>
<p><a href="<s:url action='registerInput' />">用户注册</a></p>
<p><a href="<s:url action='loginInput' />">用户登录</a></p>
<p><a href="<s:url action='sample' />">Sample Action</a></p>
<p><a href="<s:url action='sortTag' />">Sort Demo Action</a></p>
<p><a href="<s:url action='datedemo' />">输出时间信息</a></p>
<p><a href="<s:url action='iteratorTag' />">iterator Action</a></p>
</body>
</html>
Hello user.jsp
<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Hello 赵帅!</title></head>
<body>
<h2><s:property value="message" /></h2>
</body>
</html>
helloUserAction.java
package com.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloUserAction extends ActionSupport {
private String message; // 鍔ㄤ綔灞炴��
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String execute() throws Exception {
setMessage("Hello Struts User");
if (userName != null) {
setMessage(getMessage() + " " + userName);
}
return SUCCESS;
}
}
运行结果:
ConvertBean.java
package com.model;
public class ConverterBean{
private double celcius;
private double fahrenheit;
public double getCelcius(){
return (fahrenheit - 32) * 5 / 9;
}
public void setCelcius(double celcius){
this.celcius = celcius;
}
public double getFahrenheit(){
return celcius * 9 / 5 + 32;
}
public void setFahrenheit(double fahrenheit){
this.fahrenheit = fahrenheit;
}
}
beanTag.jsp
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Bean Tag Example!</title></head>
<body>
<p>Bean Tag Example</p>
<s:bean name="com.model.ConverterBean" id="converter">
<s:param name="celcius">39</s:param>
</s:bean>
39°C=<s:property value="#converter.fahrenheit"/>°F
</body>
</html>
运行结果: