Struts 学习笔记1
1.建立一个名为struts2的web project
2.需要引入的包:
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-logging-1.1.1.jar
ognl-3.0.1.jar
struts2-core-2.2.3.jar
xwork-core-2.2.3.jar
-----------------struts2.0要求导入的包,除此以外还要添加以下的包:
freemarker-2.3.16.jar
javassist-3.11.0.GA.jar
commons-lang-2.5.jar
将这些包直接复制到WebRoot/WEB-INF/lib目录下即可,Myeclipse会自动将这些包添加到class path里。
3.新建一个login.jsp
1)修改web.xml, 配置filter(org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter),即配置struts过滤器。
配置如下:
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<filter-name>struts2</filter-name>
<?xml version="1.0" encoding="UTF-8"?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<package name="struts2" extends="struts-default">
<action name="login" class="com.struts2.LoginAction">
<result name="success">/result.jsp</result>
4)在src目录下建一个包:com.struts2,并在包中建一个类:LoginAction.java,并编辑,如下:
public void setUsername(String username) {
public void setPassword(String password) {
<form action="login" class="com.struts2.LoginAction">
username:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="submit">
然后再在webRoot目录一添加一个result.jsp页面,编辑如下:
username: ${requestScope.username }<br>
username: ${param.username }<br>
<!-- 注意这一种取值方式与上面两种之间的差别,上面两种的取值方式相同 -->
password: ${requestScope.password }<br>
将struts2项目添加到tomcat应用中,修改Tomcat 6.0\conf目录下的server.xml文件
<Context path="/struts2" docBase="F:\workspace6.5\struts2\WebRoot" reloadable="true"/>
关联jar包的源文件 给某一个jar包关联源代码,在工程上点右键 -> builder path -> libaries -> 点开那个指定的包,
选择 Source attachment ,然后再选择Edit,择中源文件所在的文件夹即可。
1.新建一个struts_doc的java project,然后将需要生在doc文件的源文件复制到src目录下,并引入相对应的依赖包
2.在MyEclipse中选择project -> generate javadoc -> 选中struts_doc工程,并指定目标文件夹 -> 下一步,并给
document取一个title -> next -> finish 。 3.然后将jd2chm.exe拷贝到刚生成的帮助文档文件夹当中即可。
<form action="userAction.action">
user info:<input type="text" name="user"><br>
<input type="submit" value="submit">
public void setUsername(String username) {
public void setPassword(String password) {
<action name="userAction" class="com.struts2.UserAction">
<result name="success">/output.jsp</result>
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport
public void setUser(User user)
public String execute() throws Exception {
System.out.println("username:"+user.getUsername());
System.out.println("password:"+user.getPassword());
return SUCCESS; //SUCCESS是ActionSupport中的一个常量
新建一个com.converter的包,并在包在添加一个类UserConverter,编辑如下:
import java.util.StringTokenizer;
import ognl.DefaultTypeConverter;
public class UserConverter extends DefaultTypeConverter
public Object convertValue(Map context, Object value, Class toType)
if(User.class==toType) //从页面向后台对象转换
StringTokenizer st=new StringTokenizer(firstValue,";");
String username=st.nextToken();
String password=st.nextToken();
else if(String.class==toType) //从后台对象向页面转换
String username=user.getUsername();
String password=user.getPassword();
String userInfo="username:"+username+", password:"+password;
然后在com.struts2包中建立一个UserAction-conversion.properties文件,并编辑:
user=com.converter.UserConverter
<s:property value="user"/><!-- 获得UserAction当中的user属性 -->
在com.converter包下再新建一个UserConverter2类,并继承StrutsTypeConverter,编辑:
import java.util.StringTokenizer;
import org.apache.struts2.util.StrutsTypeConverter;
public class UserConverter2 extends StrutsTypeConverter {
public Object convertFromString(Map context, String[] values, Class toClass) {
StringTokenizer st=new StringTokenizer(value,";");
user.setUsername(st.nextToken());
public String convertToString(Map context, Object o) {
String username=user.getUsername();
String password=user.getPassword();
String userInfo="username:"+username+",password:"+password;
并修改UserAction-Converter.properties,如下:
#user=com.converter.UserConverter
user=com.converter.UserConverter2
<form action="usersAction.action">
user info1:<input type="text" name="users"><br>
user info2:<input type="text" name="users"><br>
user info3:<input type="text" name="users"><br>
user info4:<input type="text" name="users"><br>
<input type="submit" value="submit">
import com.opensymphony.xwork2.ActionSupport;
public class UsersAction extends ActionSupport
public List<User> getUsers() {
public void setUsers(List<User> users) {
public String execute() throws Exception {
System.out.println(u.getUsername()+", "+u.getPassword());
import java.util.StringTokenizer;
import org.apache.struts2.util.StrutsTypeConverter;
public class UsersConverter extends StrutsTypeConverter
public Object convertFromString(Map context, String[] values, Class toClass) {
List<User> list=new ArrayList<User>();
StringTokenizer st=new StringTokenizer(value,";");
user.setUsername(st.nextToken());
user.setPassword(st.nextToken());
public String convertToString(Map context, Object o) {
List<User> list=(List<User>)o;
StringBuffer buffer=new StringBuffer();
String username=user.getUsername();
String password=user.getPassword();
buffer.append("username:").append(username)
.append(" password:").append(password).append(" ");
UsersAction-Converter.properties
users=com.converter.UsersConverter
<s:property value="user"/><!-- 获得UserAction当中的user属性 -->