[关闭]
@snail-lb 2016-07-17T12:08:49.000000Z 字数 5842 阅读 952

struts2自学笔记

一. 建立我的第一个struts2工程

1. 创建JavaWeb动态工程

2. 导入struts2 jar包

3. 修改web.xml文件

```
<?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>


```

4. 在src目录下创建struts.xml文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <!--constant 配置用于开启动态方法调用 -->
  7. <constant name="struts.enable.DynamicMethodInvocation" value="true" />
  8. <package name="default" extends="struts-default" >
  9. <action name="login" class="struts2_deoml.LoginAction">
  10. <result name="success">/success.jsp</result>
  11. <result name="fail">/ShuChu.jsp</result>
  12. </action>
  13. </package>
  14. </struts>

5.创建LoginAction.java

  1. package struts2_deoml;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class LoginAction extends ActionSupport{
  4. private static final long serialVersionUID = 1L;
  5. @Override
  6. public String execute() throws Exception {
  7. System.out.println("执行Action");
  8. return SUCCESS;
  9. }
  10. }

6.jsp表单跳转配置

  1. <form action="login" method="post">

7.创建其他相应的jsp页面,提交login.jsp中的表单即可运行成功。

二. Struts2方法调用的三种方式

1.指定method属性

  1. <action name="UserMethod" class="struts2_deoml.UserMethodAction" method="add">
  2. <result name="add">/add_user.jsp</result>
  3. </action>

2.动态方法调用(DMI)

jsp提交写法

  1. <a href="User!add">添加用户</a>

struts.xml配置中必须设置这个常量才能保证正常使用动态方法调用

  1. <constant name="struts.enable.DynamicMethodInvocation" value="true" />

struts.xml中action配置

  1. <action name="User" class="struts2_deoml.UserAction">
  2. <result name="add">/add_user.jsp</result>
  3. <result name="update">/update_user.jsp</result>
  4. </action>

UserAction.java

  1. public class UserAction extends ActionSupport{
  2. private static final long serialVersionUID = 1L;
  3. //添加用户
  4. public String add(){
  5. System.out.println("添加用户!!!!");
  6. return "add";
  7. }
  8. }

3.通配符(推荐使用)

  1. <action name="UserClMethod*" class="struts2_deoml.UserClMethodAction" method="{1}">
  2. <result name="{1}">/{1}_user.jsp</result>
  3. </action>
  1. <a href="UserClMethodadd">添加用户(通配符调用方法)</a>

三.Struts2接收参数的几种方式

1.在action里定义要接收的参数,并提供相应的set和get方法。

Action类:

  1. //LoginAction.java
  2. public class LoginAction extends ActionSupport{
  3. private static final long serialVersionUID = 1L;
  4. private String username;
  5. private String password;
  6. @Override
  7. public String execute() throws Exception {
  8. System.out.println("执行Action");
  9. System.out.println("username:" + username + " password:" + password);
  10. return SUCCESS;
  11. }
  12. public String getUsername() {
  13. return username;
  14. }
  15. public void setUsername(String username) {
  16. this.username = username;
  17. }
  18. public String getPassword() {
  19. return password;
  20. }
  21. public void setPassword(String password) {
  22. this.password = password;
  23. }
  24. }

Jsp提交方式

  1. <form action="login" name="form1" method="post">
  2. 用户:<input type="text" name="username"><br>
  3. 密码:<input type="password" name="password"><br>
  4. <input type="submit" value="提交" > <input type="reset" value="清空" >
  5. </form>

2.使用DomainModel

如果Action中属性过多,则可以Model保存为一个对象,并提供get和set
Model类:

  1. public class User {
  2. private String username;
  3. private String password;
  4. public String getUsername() {
  5. return username;
  6. }
  7. public void setUsername(String username) {
  8. this.username = username;
  9. }
  10. public String getPassword() {
  11. return password;
  12. }
  13. public void setPassword(String password) {
  14. this.password = password;
  15. }
  16. }

Action类

  1. public class LoginAction2 extends ActionSupport{
  2. private static final long serialVersionUID = 1L;
  3. User user = new User();
  4. @Override
  5. public String execute() throws Exception {
  6. System.out.println("执行LoginAction2");
  7. System.out.println("username:" + user.getUsername() +
  8. " password:" + user.getPassword());
  9. return SUCCESS;
  10. }
  11. public User getUser() {
  12. return user;
  13. }
  14. public void setUser(User user) {
  15. this.user = user;
  16. }
  17. }

Jsp提交方式:

  1. <form action="login2" name="form2" method="post">
  2. 用户:<input type="text" name="user.username"><br>
  3. 密码:<input type="password" name="user.password"><br>
  4. <input type="submit" value="提交" > <input type="reset" value="清空" >
  5. </form>

3.使用ModelDriven(模型驱动)

使用模型驱动模式时,Acton必须实现ModelDriven接口,实现该接口则必须实现getModel()方法,该方法用于把Action和与之对应的Model实例关联起来。
Model类同上。

Action类:

  1. public class LoginAction3 extends ActionSupport implements ModelDriven<User>{
  2. private static final long serialVersionUID = 1L;
  3. User user = new User();
  4. @Override
  5. public String execute() throws Exception {
  6. System.out.println("执行LoginAction3");
  7. System.out.println("username:" + user.getUsername() +
  8. " password:" + user.getPassword());
  9. return SUCCESS;
  10. }
  11. public User getUser() {
  12. return user;
  13. }
  14. public void setUser(User user) {
  15. this.user = user;
  16. }
  17. public User getModel() {
  18. return user;
  19. }
  20. }

JSP提交方式:(这里提交既可以用 “对象.变量” 的方式提交,又可以直接用 “变量” 的方式提交)

  1. <form action="login3" name="form3" method="post">
  2. 用户:<input type="text" name="user.username"><br>
  3. 密码:<input type="password" name="password"><br>
  4. <input type="submit" value="提交" > <input type="reset" value="清空" >
  5. </form>

4.使用request接收参数

此方法和传统的JSP页面接收参数一样,用request.getParameter("")方法。
Action类:

  1. ActionContext context=ActionContext.getContext();
  2. HttpServletRequest request = (HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);
  3. String username_2 = request.getParameter("username");
  4. String password_2 = request.getParameter("password");
  5. System.out.println("username:" + username_2 + " password:" + password_2);

JSP 提交方式:

  1. <s:form action="login3" method="post">
  2. <s:textfield name="username" label="用户名"/>
  3. <s:password name="password" label="密码"/>
  4. <s:submit/>
  5. </s:form>

5.使用ActionContext得到parameterMap获取参数

  1. //方法二:在Action中使用ActionContext得到parameterMap获取参数:
  2. ActionContext context=ActionContext.getContext();
  3. Map<?,?> parameterMap=context.getParameters();
  4. String username[]=(String[])parameterMap.get("username");
  5. String password[]=(String[])parameterMap.get("password");
  6. System.out.println("username: " +username[0] + "password: " +password[0]);

JSP 提交方式:

  1. <s:form action="login3" method="post">
  2. <s:textfield name="username" label="用户名"/>
  3. <s:password name="password" label="密码"/>
  4. <s:submit/>
  5. </s:form>

四.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注