struts2学习札记(1)

开发步骤
1导入struts2.x所需的jar包,common-logging.jar freemaker.jar ognl.jar struts-core.jar
xwork.jar
common-logging.jar 是日志包
freemarker.jar     是提供前台模板包
ongl.jar           是类似jstl语言,但比jstl语言更强大
struts-core.jar    核心包
xwork.jar       

2
再web.xml配置struts2
<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
struts1.x与struts2.x一个很大区别是struts1.x是servlet,而struts2.x是filter,所以struts1.x是单实例,
3写再struts.xml
<struts>
      <package name="com.struts2" extends="struts-default">
        <action name="reg" class="com.struts2.HelloWorld">
           <result name="success">/index.jsp</result>
           <result name="error">/login.jsp</result>
        </action>
      </package>
    </struts>

4写action
HelloWorld.java
package com.struts2;

public class HelloWorld {
   public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String username;
   private  int age;
   public String execute(){
if(this.getUsername().equals(""))
return "error";
else {
System.out.println ("姓名"+getUsername()+"\t 年龄:"+this.getAge());
     return "success";
}

  
   }
}
5写一个简单页面login.jsp,使用struts2.0能否正常运行
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<body>
    <s:form method="post" action="reg">
    <p>
    <s:textfield name="username" label="姓名"></s:textfield>
    </p>
    <p><s:textfield name="age" label="年龄"></s:textfield></p>
    <p>
    <s:submit value="提交"></s:submit>
    </p>
    </s:form>
  </body>
</html>
简要的说明:s标签的name,必须和action属性要一样

struts2.x可以像struts1.x写个FormBean
例如
package com.struts2;

import java.util.Date;

public class User {
private Date date2;
  
public Date getDate2() {
return date2;
}
public void setDate2(Date date2) {
this.date2 = date2;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String username;
    private  int age;
}
action:
import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class HelloWorld implements Action{
   private User user=null;
   public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@SuppressWarnings("unchecked")
public String execute(){
   Map<String,String> map=null;
if(user.getUsername().equals(""))
return "error";
else {
map=ActionContext.getContext().getSession();
map.put("username", user.getUsername());
System.out.println ("姓名"+user.getUsername()+"\t 年龄:"+user.getAge()+"\t注册日期"+user.getDate2());
     return "success";
}

  
   }
}
再jsp这样写
<s:form method="post" action="reg" id="id">
    <p>
    <s:textfield name="user.username" label="username"></s:textfield>
    </p>
    <p><s:textfield name="user.age" label="age"></s:textfield></p>
    <p><s:datetimepicker name="user.date2" label="date2" displayFormat="yyyy年MM月dd日"></s:datetimepicker></p>
    <p>
    <s:submit value="提交"></s:submit>
    </p>
    </s:form>

你可能感兴趣的:(jsp,freemarker,xml,servlet,struts)