花了几天时间上手了jsp和servlet,决定继续学习struts2,噩梦啊,配置了好久总结一下,重要的地方就加粗用红字标记了:
Struts 2 环境配置
第一步:安装JDK,Apache Tomcat,Eclipse(IDE)。(
这些都不是重点)
第二步:配置Struts2 Libraries
- 上Apache官网下载自己对应操作系统的Struts2版本。我这边下的是struts-2.3.3-all.zip,解压之后看到的
![](https://img-my.csdn.net/uploads/201205/16/1337155352_8615.jpg)
- 打开Eclipse,File->New->Project->Dynamic Web Project,命名为HelloWorldStruts2
- 将刚解压的文件lib文件夹下的下列包添加到新建项目WEB-INF\lib文件加下并导入相应的包
x.y.z对应不同版本Struts的版本号
- commons-fileupload-x.y.z.jar
- commons-io-x.y.z.jar
- commons-lang-x.y.jar
- commons-langx-y.z
- commons-logging-x.y.z.jar
- commons-logging-api-x.y.jar
- freemarker-x.y.z.jar
- javassist-.xy.z.GA
- ognl-x.y.z.jar
- struts2-core-x.y.z.jar
- xwork-core.x.y.z.jar
- 在Java Resources > src文件夹下New > Package 包名为 com.tutorialspoint.struts2
- 在包内新建一个类命名为HelloWorldAction 类中代码如下:
package com.tutorialspoint.struts2; public class HelloWorldAction { private String name; public String execute() throws Exception { return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
- 在WebContent/文件夹下新建一个Jsp页面 New > JSP File 命名为HelloWorld.jsp 代码如下:
<?xml version="1.0" encoding="GB18030" ?> <%@ page language="java" contentType="text/html;charset=GB18030" pageEncoding="GB18030"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>HelloWorld</title> </head> <body> Hello World, <s:property value="name"/> </body> </html>
- 同样的再在WebContent/下新建一个JSP页面 名字为index.jsp 代码如下:
<?xml version="1.0" encoding="GB18030" ?> <%@ page language="java" contentType="text/html;charset=GB18030" pageEncoding="GB18030"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030" /> <title>Insert title here</title> </head> <body> <h1>Hello World From Struts2</h1> <form action="hello"> <label for="name">Please enter your name</label><br/> <input type="text" name="name"/> <input type="submit" value="Say Hello"/> </form> </body> </html>
- 在WebContent文件夹下找到/WEB-INF/web.xml文件修改配置如下:
<?xml version="1.0" encoding="GB18030"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>struts2</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list ></web-app>
- 在Java Resources/src/下新建一个XML文件,命名为struts.xml,配置如下:
<?xml version="1.0" encoding="GB18030"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="helloworld" extends="struts-default" > <action name="hello" class="com.tutorialspoint.struts2.HelloWorldAction" method="execute"> <result name="success">/HelloWorld.jsp</result> </action> </package> </struts>
- 启动服务器,浏览相应的页面,如下图:
关于中文乱码
Tomcat默认使用的是ISO-8859-1的编码方式,页面中申明的是GB320,所以使用doGet方式获取name时要进行编码转换
例子如下:
public String getName() {
try{
String name2= new String(name.getBytes("ISO-8859-1"),"GB18030");
name=name2;
}catch(Exception ex){
System.out.println(ex.getMessage());
}
return name;
}
就能显示中文了