一个简单的login登录界面,这也有问题,那也有问题,真是揪心,由于是手动配置,所以很多细节方面都出现了问题,就自己所犯的错误,总结经验:
1:index.xml文件放在WebRoot的根目录下,如果放在WEB-INF下,即使web.xml中配置了路径加名称也不行。
2:在输入界面的action中,输入的是对应的动作,要加上工程名称:
Action=”/Login/login.do”
相同颜色,对应相同关系
3:<form-beanname="userform"type="com.ypj.form.UserForm">
<action path="/login" name="userform"type="com.ypj.action.LoginAction">
4:处理乱码问题需要配置过滤器在web.xml,还有过滤器类:
要放在开始的地方,最前面
<filter>
<filter-name>setEncoding</filter-name>
<filter-class>com.ypj.Filter.FilterEncoding</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
packagecom.ypj.Filter;
importjava.io.IOException;
importjavax.servlet.Filte;
importjavax.servlet.FilterChain;
importjavax.servlet.FilterConfig;
importjavax.servlet.ServletConfig;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRequest;
importjavax.servlet.ServletResponse;;
publicclass FilterEncoding implements Filter {
private FilterConfig filterConfig = null;
private String encoding = null;
@Override
public void destroy() {
// TODO Auto-generated methodstub
this.encoding = null;
this.filterConfig = null;
}
public void init(ServletConfig arg0)throws ServletException {
// TODO Auto-generated methodstub
this.filterConfig = filterConfig ;
this.encoding =filterConfig.getInitParameter("encoding");
}
@Override
public void doFilter(ServletRequestrequest, ServletResponse response,
FilterChain chain)throws IOException, ServletException {
// TODO Auto-generated methodstub
String encoding =this.encoding;
if(encoding != null)
{
request.setCharacterEncoding(encoding);
}
chain.doFilter(request,response);
}
@Override
public void init(FilterConfigfilterConfig) throws ServletException {
// TODO Auto-generated methodstub
this.filterConfig =filterConfig;
this.encoding =filterConfig.getInitParameter("encoding");
}
}
5:在函数里面点击alt+/可以选择函数中自带的方法,若出现错误,把鼠标放到出现红色波浪线的上面,会提示需要导入包或者别的动作,单击即可。
6:Action 中是方法,执行的动作,ActionForm 是表单
7:配置servlet:
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
包的路径
org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value> 注意路径
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>