TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
1. folder structure src
|--sturts.xml
|--struts.property
|--user
|-- user.xml
|-- User.java
|-- UserAction.java
|-- BizService.java
WebContent
|-- user
|-- index.jsp
|-- UserForm.jsp
|-- UserList.jsp
|-- WEB-INF
|-- web.xml
|-- lib
|-- ... (All the need jar packages of Struts2.0)
2. struts.xml

<!
DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>


<
struts
>

<
include
file
="user/user.xml"
/>


</
struts
>
3. struts.property
Notice: If you have config these two constants in struts.xml, you shouldn"t config them here any more, or there will be errors report.

struts.devMode
=
true
struts.enable.DynamicMethodInvocation
=
true
4. user.xml

<!
DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>


<
struts
>

<
package
name
="user"
namespace
="/user"
extends
="struts-default"
>
<
action
name
="crud!*"
method
="{1}"
class
="user.UserAction"
>
<
result
name
="input"
>
/user/UserForm.jsp
</
result
>
<
result
name
="list"
>
/user/UserList.jsp
</
result
>
<
result
name
="success"
type
="redirect-action"
>
crud!list.action
</
result
>
</
action
>

</
package
>

</
struts
>
5. User.java

package
user;

 
public
class
User
...
{

private String userid;
private String username;
private String password;
 /** *//**
* @return the password
*/
 public String getPassword() ...{
return password;
}
 /** *//**
* @param password the password to set
*/
 public void setPassword(String password) ...{
this.password = password;
}
 /** *//**
* @return the userid
*/
 public String getUserid() ...{
return userid;
}
 /** *//**
* @param userid the userid to set
*/
 public void setUserid(String userid) ...{
this.userid = userid;
}
 /** *//**
* @return the username
*/
 public String getUsername() ...{
return username;
}
 /** *//**
* @param username the username to set
*/
 public void setUsername(String username) ...{
this.username = username;
}

}
6. UserAction.java

package
user;


import
java.util.ArrayList;

import
java.util.List;


import
com.opensymphony.xwork2.ActionSupport;

 
public
class
UserAction
extends
ActionSupport
...
{
private User user = new User();
private List<User> users;
 public String list() ...{
BizService bizService = new BizService();
users = bizService.getUsers();
return "list";
}
 public String add() ...{
BizService bizService = new BizService();
 if(bizService.add(user)) ...{
return SUCCESS;
 } else ...{
return INPUT;
}
}
 /** *//**
* @return the users
*/
 public List getUsers() ...{
return users;
}

 /** *//**
* @param users the users to set
*/
 public void setUsers(List<User> users) ...{
this.users = users;
}
 /** *//**
* @return the user
*/
 public User getUser() ...{
return user;
}

 /** *//**
* @param user the user to set
*/
 public void setUser(User user) ...{
this.user = user;
}
}
7. BizService.java
This is just a example, but does not do actual work indeed.

package
user;


import
java.util.ArrayList;

import
java.util.List;

 
public
class
BizService
...
{
 public List getUsers() ...{
List<User> alUser = new ArrayList();
User u1 = new User();
u1.setUserid("1");
u1.setUsername("lwp1");
u1.setPassword("password1");
User u2 = new User();
u2.setUserid("2");
u2.setUsername("lwp2");
u2.setPassword("password2");
alUser.add(u1);
alUser.add(u2);
return alUser;
}
 public boolean add(User user) ...{
//Do actual addition here
return true;
}
}

8. index.jsp
Two links: one used to show list action, and the other to show add action.


<%
...
@ page contentType="text/HTML; charset=UTF-8"
%>
 
<%
...
@ taglib prefix="s" uri="/struts-tags"
%>
 
<%
...
--
@ author: Newweapon
@ date: 2007-5-4, ??02:47:36
--
%>

<
html
>

<
head
>

<
title
>
Struts 2.0
</
title
>

</
head
>

<
body
>

<
p
>
<
a
href
="<s:url action="
crud!list"
/>
"> list users
</
a
>
</
p
>

<
p
>
<
a
href
="UserForm.jsp"
>
add a user
</
a
>
</
p
>

</
body
>

</
html
>
9. UserForm.jsp


<%
...
@ page contentType="text/html; charset=UTF-8"
%>
 
<%
...
@ taglib prefix="s" uri="/struts-tags"
%>
 
<%
...
--
@ author: Newweapon
@ date: 2007-5-4, ??06:20:09
--
%>

<
html
>

<
head
>

<
title
>
User Form
</
title
>

</
head
>

<
body
>

<
h2
>
User Form
</
h2
>

<
table
>

<
s:form
action
="crud!add.action"
method
="post"
>
<
s:textfield
name
="user.userid"
value
="%{user.userid}"
label
="UserID"
size
="20"
/>
<
s:textfield
name
="user.username"
value
="%{user.username}"
label
="UserName"
size
="20"
/>
<
s:textfield
name
="user.password"
value
="%{user.password}"
label
="Password"
size
="20"
/>
<
s:submit
value
="submit"
/>

</
s:form
>

</
body
>

</
html
>
10. UserList.jsp


<%
...
@ page contentType="text/html; charset=UTF-8"
%>
 
<%
...
@ taglib prefix="s" uri="/struts-tags"
%>
 
<%
...
--
@ author: Newweapon
@ date: 2007-5-4, pm 03:11:41
--
%>

<
html
>

<
head
>

<
title
>
User List
</
title
>

</
head
>

<
body
>

<
h2
>
User List
</
h2
>

<
table
>

<
tr
>
<
td
>
UserID
</
td
>
<
td
>
UserName
</
td
>
<
td
>
Password
</
td
>

</
tr
>

<
s:iterator
value
="users"
status
="status"
>

<
tr
>
<
td
><
s:property
value
="userid"
/></
td
>
<
td
><
s:property
value
="username"
/></
td
>
<
td
><
s:property
value
="password"
/></
td
>

</
tr
>

</
s:iterator
>

</
table
>

<
p
><
a
href
="index.jsp"
>
Back to index.jsp
</
a
></
p
>

</
body
>

</
html
>
11. web.xml

<?
xml version="1.0" encoding="UTF-8"
?>

<
web-app
id
="WebApp_9"
version
="2.4"
xmlns
="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>

<
display-name
>
ebookhouse
</
display-name
>
<
filter
>
<
filter-name
>
struts2
</
filter-name
>
<
filter-class
>
org.apache.struts2.dispatcher.FilterDispatcher
</
filter-class
>
</
filter
>
<!--
The webapplication"s descriptor file contains one filter and its mapping.
By default, the filter is mapped to /*, meaning all requests will be
intercepted, but only those ending with a specific suffix (.action,
by default) and certain special paths (for static files) will be processed
and handled by WebWork
-->
<
filter-mapping
>
<
filter-name
>
struts2
</
filter-name
>
<
url-pattern
>
/*
</
url-pattern
>
</
filter-mapping
>

<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>
<
welcome-file-list
>
<
welcome-file
>
index.html
</
welcome-file
>
<
welcome-file
>
index.htm
</
welcome-file
>
<
welcome-file
>
index.jsp
</
welcome-file
>
</
welcome-file-list
>

</
web-app
>
12. OK. Now input http://localhost:8080/yoursite/user/index.jsp into your browser and enjoy it! |
|