项目使用的框架是strurts2+spring+hibernate,现在要实现的功能是输入邮政编码,查询出所属地。
1.后台如何获取前台用户输入的邮政编码
2.后台获取邮政编码后如何连接到数据库并实现查询语句
3.如何获取查询结果并返回前台进行显示1.直接后台Action里一个public postCode pc;然后后台的邮编标签name为pc.name再submit就可以实现接收数据了吗?
2.是在service里写个方法吧,具体怎样写
3.实现后准备打算再改成dwr显示求高人指点,谢谢
1.后台如何获取前台用户输入的邮政编码
2.后台获取邮政编码后如何连接到数据库并实现查询语句
3.如何获取查询结果并返回前台进行显示1.直接后台Action里一个public postCode pc;然后后台的邮编标签name为pc.name再submit就可以实现接收数据了吗?
2.是在service里写个方法吧,具体怎样写
3.实现后准备打算再改成dwr显示求高人指点,谢谢
如果你想用Struts2 的标签的话就需要在页面上加上一条page指令:<%@ taglib prefix="s" uri="/struts-tags" %>
<s:form name="form1" action="postc.action" method="post">
<s:textfield name="pc.name" label="邮编"></s:textfield> <s:submit name="sub" value="提交"></s:submit>
</s:form>页面有了、点击提交的时候、需要在Struts配置文件中写一段配置:<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.ui.theme" value="simple"></constant>
<package name="pcTest" extends="struts-default">
<!--如果你用Spring的话、就把Action托管给Spring把class写上你Spring bean的ID、不托管的话、就直接写你类的完全限定名、我这就写完全限定名吧-->
<action name="postc" class="com.test.action.PostCodeAction.class">
<!-- 在这里就写的是返回你的查询结果并返回前台进行显示、的页面 -->
<result>/postList.jsp</result>
</action>
</package>
</struts>
web.xml配置:<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <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> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext*.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
下面是你的javabean这个就不用写了、就是你的postCode类~~
然后是你的Action:PostCodeAction(假设是这个名字):需要继承ActionSupport类
o(︶︿︶)o 唉太多了~~~
像你说的、Action里面需要一个postCode pc的属性、并生成get/set方法。
需要业务层的属性注入、如果你用注解的话、就要写注解。
接住面向接口编程、所有的注入都是注入接口、而不是实现类!
业务层呢~~~写一个接口、一个实现类、实现类注入数据层的接口
数据层呢~~~也写一个接口、一个实现类、实现类继承HibernateDaoSupport类
在Action中查询出来所有数据过后~~~
可以用Struts 的 ognl 跟迭代标签在页面进行显示!!!
o(︶︿︶)o 唉太多了~~~不可能你什么都不知道吧???还有就注意jar包的冲突有没有、或者是少没少!!!
那么就调用Service层里面的方法了~~~Service层里面的方法又调用dao层的方法~~~
如果你有公共的dao代码~~~就万事OK了!!!没有的话~~~我博客写有一个~~~
是Hiberntae的!地址是:http://blog.csdn.net/hzw2312/archive/2011/05/05/6396590.aspx
如果你的Spring配置文件也配置好了、并且该注入的也注入了~~~那么就开始写Service接口跟实现类。<!-- 数据访问类,注入SessionFactory -->
<bean id="Dao_HqlImp" class="com.boxun.crm.dao.impl.Dao_HqlImp" scope="singleton">
<!-- ref中写的就是你的SessionFactory id名 -->
<property name="sessionFactory" ref="mySessionFactory"></property>
</bean>
<bean id="Dao_SqlImp" class="com.boxun.crm.dao.impl.Dao_SqlImp" scope="singleton">
<property name="sessionFactory" ref="mySessionFactory"></property>
</bean>
<!-- Biz -->
<bean id="postCodeBizImp" class="com.boxun.crm.biz.impl.PostCodeBizImp">
<property name="baseDao" ref="Dao_HqlImp"></property>
</bean>
<!-- Action -->
<bean id="postCodeAction" class="com.boxun.crm.action.PostCodeAction">
<property name="postBiz" ref="postCodeBizImp"></property>
</bean>分别在实现类中注入:
创建一个Service接口名为:IPostCodeBiz:package com.boxun.crm.service;public interface IPostCodeBiz { /**
* 根据邮编查询归属地
* @param youbian
* @return
*/
public String getAddresByPost(String post);
}
创建一个Service实现类实现刚刚创建的Service接口名为:PostCodeBizImpl:public class PostCodeBizImpl implements IPostCodeBiz { private IDao_Sql dao_Sql;
public void setDao_Sql(IDao_Sql daoSql) {
dao_Sql = daoSql;
} public String getAddresByPost(String post) {
String sql = "select address from tableName where post = " +post ;
Object objStr = dao_Sql(sql).get(0);
if(objStr != null && !objStr.equals("")){
return objStr.toString();
}else{
return "没有数据";
}
}}在Action中像Service调用Dao一样把调用Service层你写的方法、记得注入~~~ private IPostCodeBiz postBiz;
public void setPostBiz(IPostCodeBiz postBiz) {
this.postBiz = postBiz;
}
private PostCode pc;
public PostCode getPc() {
return pc;
}
public void setPc(PostCode pc) {
this.pc = pc;
}
public String execute(){
String addres = postBiz.getAddresByPost(pc.getName()); //在页面可以直接addres获取邮编归属地名
ActionContext.getContext().put("addres", addres);
/**除了这种方法还可以在页面直接获取归属地
* pc.setAddres(addres);
* 这样就不用ActionContext来设置addres的值了
*/
//记住配置你自己的success页面
return SUCCESS;
}
然后在页面 <h1><s:property value="pc.addres" /> </h1> 或者是<h1><s:property value="addres" /> </h1>
前者是不用ActionContext设值的用法、后者是用ActionContext设值的用法LZ将就看吧~~~不知道是不是你想要的~~~
还有就是你的邮编跟归属地数据库中有对应的数据没有~~~