@snail-lb
2016-07-17T12:08:49.000000Z
字数 5842
阅读 952
```
<?xml version="1.0" encoding="UTF-8"?>
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
struts2_3
Struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
org.apache.struts2.dispatcher.FilterDispatcher
org.apache.struts2.dispatcher.FilterDispatcher
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>org.apache.struts2.dispatcher.FilterDispatcher</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
```
<?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 配置用于开启动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="default" extends="struts-default" >
<action name="login" class="struts2_deoml.LoginAction">
<result name="success">/success.jsp</result>
<result name="fail">/ShuChu.jsp</result>
</action>
</package>
</struts>
package struts2_deoml;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
System.out.println("执行Action");
return SUCCESS;
}
}
6.jsp表单跳转配置
<form action="login" method="post">
7.创建其他相应的jsp页面,提交login.jsp中的表单即可运行成功。
<action name="UserMethod" class="struts2_deoml.UserMethodAction" method="add">
<result name="add">/add_user.jsp</result>
</action>
jsp提交写法
<a href="User!add">添加用户</a>
struts.xml配置中必须设置这个常量才能保证正常使用动态方法调用
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
struts.xml中action配置
<action name="User" class="struts2_deoml.UserAction">
<result name="add">/add_user.jsp</result>
<result name="update">/update_user.jsp</result>
</action>
UserAction.java
public class UserAction extends ActionSupport{
private static final long serialVersionUID = 1L;
//添加用户
public String add(){
System.out.println("添加用户!!!!");
return "add";
}
}
<action name="UserClMethod*" class="struts2_deoml.UserClMethodAction" method="{1}">
<result name="{1}">/{1}_user.jsp</result>
</action>
<a href="UserClMethodadd">添加用户(通配符调用方法)</a>
Action类:
//LoginAction.java
public class LoginAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private String username;
private String password;
@Override
public String execute() throws Exception {
System.out.println("执行Action");
System.out.println("username:" + username + " password:" + password);
return SUCCESS;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Jsp提交方式
<form action="login" name="form1" method="post">
用户:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="提交" > <input type="reset" value="清空" >
</form>
如果Action中属性过多,则可以Model保存为一个对象,并提供get和set
Model类:
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Action类
public class LoginAction2 extends ActionSupport{
private static final long serialVersionUID = 1L;
User user = new User();
@Override
public String execute() throws Exception {
System.out.println("执行LoginAction2");
System.out.println("username:" + user.getUsername() +
" password:" + user.getPassword());
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
Jsp提交方式:
<form action="login2" name="form2" method="post">
用户:<input type="text" name="user.username"><br>
密码:<input type="password" name="user.password"><br>
<input type="submit" value="提交" > <input type="reset" value="清空" >
</form>
使用模型驱动模式时,Acton必须实现ModelDriven接口,实现该接口则必须实现getModel()方法,该方法用于把Action和与之对应的Model实例关联起来。
Model类同上。
Action类:
public class LoginAction3 extends ActionSupport implements ModelDriven<User>{
private static final long serialVersionUID = 1L;
User user = new User();
@Override
public String execute() throws Exception {
System.out.println("执行LoginAction3");
System.out.println("username:" + user.getUsername() +
" password:" + user.getPassword());
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public User getModel() {
return user;
}
}
JSP提交方式:(这里提交既可以用 “对象.变量” 的方式提交,又可以直接用 “变量” 的方式提交)
<form action="login3" name="form3" method="post">
用户:<input type="text" name="user.username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="提交" > <input type="reset" value="清空" >
</form>
此方法和传统的JSP页面接收参数一样,用request.getParameter("")方法。
Action类:
ActionContext context=ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);
String username_2 = request.getParameter("username");
String password_2 = request.getParameter("password");
System.out.println("username:" + username_2 + " password:" + password_2);
JSP 提交方式:
<s:form action="login3" method="post">
<s:textfield name="username" label="用户名"/>
<s:password name="password" label="密码"/>
<s:submit/>
</s:form>
//方法二:在Action中使用ActionContext得到parameterMap获取参数:
ActionContext context=ActionContext.getContext();
Map<?,?> parameterMap=context.getParameters();
String username[]=(String[])parameterMap.get("username");
String password[]=(String[])parameterMap.get("password");
System.out.println("username: " +username[0] + "password: " +password[0]);
JSP 提交方式:
<s:form action="login3" method="post">
<s:textfield name="username" label="用户名"/>
<s:password name="password" label="密码"/>
<s:submit/>
</s:form>