1、首先下载Struts2GA版本,GA的全称是General Availability。
使用Struts2必须的包:core、xwork、freemarker、ognl。
2、配置web.xml文件
- <filter>
- <filter-name>struts2filter-name>
- <filter-class>
- org.apache.struts2.dispatcher.FilterDispatcher
- filter-class>
- filter>
- <filter-mapping>
- <filter-name>struts2filter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
3、建立JSP
- <%@ page language="java" contentType="text/html; charset=UTF-8"%>
- <%@taglib prefix="s" uri="/struts-tags" %>
- >
- <html>
- <head>
- <title>struts2 testDemotitle>
- head>
- <body>
- <s:form action="login.action" method="post">
- <s:textfield key="username" name="user.username"/>
- <s:password key="password" name="user.password" />
- <s:submit key="submit" />
- s:form>
- body>
- html>
4、建立Action
- package cc.dynasoft.action;
- import cc.dynasoft.vo.User;
- public class LoginAction
- {
- private User user;
- public User getUser()
- {
- return user;
- }
- public void setUser(User user)
- {
- this.user = user;
- }
- public String execute() throws Exception
- {
- if(user.getUsername().equals("chenshuang") && user.getPassword().equals("chenshuang"))
- {
- return "success";
- }
- else
- {
- return "error";
- }
- }
- }
User.java
- package cc.dynasoft.vo;
- 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;
- }
- }
5、配置struts.xml
- 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">
- <struts>
- <package name="login" extends="struts-default">
- <action name="login" class="cc.dynasoft.action.LoginAction">
- <result name="success">/welcome.jspresult>
- <result name="error">/login.jspresult>
- action>
- package>
- struts>
6、完成国际化
在struts.xml文件中加上配置
- <constant name="struts.custom.i18n.resources" value="message" />
建立资源文件message_en_US.properties和message_zh_CN.properties
message_en_US.properties
- username=username
- password=password
- welcome=welcom to
- submit=submit
message_zh_CN.properties
- username=\u7528\u6237\u540d
- password=\u5bc6\u7801
- welcome=\u6b22\u8fce
- submit=\u63d0\u4ea4